Running a java program in another java code
Hello,
I have a question.I wrote a java server code named server.java. In this server code, I receive another java class code called "HelloWorld.class" from another node. What I want to do is to execute the class HelloWorld inside server.java.
Briefly, I would like to run a compiled java code (class code)in another executing class code. How can I achieve this?
Thanks in advance
�ebnem Bora
Hi,
Sorry but your terminology isn't that clear. Are you trying:
1. To call methods on another class or
2. To launch a separate process that will run another class?
If it's either of these then they're not 'Advanced Language Topics' but simple:
// Scenario 1
// in Server class
HelloWorld hw = new HelloWorld();
hw.sayHello(); // or whatever the method is
// Scenario 2
Runtime.exec(new String [] {"java", "HelloWorld"}); // or whateverIs that of any help?
Dom.
Similar Messages
-
Please help with executing a java program from another java program
Hi, I have tried to execute a very simple java program which is printed out "Hello world" from another java gui program when the user click on the "START" button. However, I don't receive any print out on the terminal when they click it. These two programs are being developed in Unix environment. Here is the code that I am trying to use.
Runtime r = Runtime.getRuntime();
r.exec("java HelloWorld");
p.s. HelloWorld is java class of the HelloWorld.javaI think you have to luanch your MS Prompt first before executing the command.
-
Compiling one java program by another
hi ppl
how to compile a java program by another java program by using javac command.
that is prog A must compile prog B using " javac B.java" commandTry com.sun.tools.javac.Main.
You require JDK_HOME/lib/tools.jar in CLASSPATH. -
How to run a java program in another folder
Hello!
I want to run for example the java class file in the path ./sourcecode/javafile.class. It works fine if I do the following:
cd sourcecode
java javafile
but it cannot work if I do:
java sourcecode/java
The error it outputs is NoClassDefFoundError.
Can someone tell me how to run a java program in another folder
thanks a lot.You need to specify the classpath using the -cp flag when running. so instead of
"java sourcecode/javafile"
Try
"java -cp sourcecode javafile" -
How to compile and run a .java file from another java program
hello,
can any one tell me how to compile and run a *.java* file from another java program which is not in same directory?Well a smarter way of implementing this is by using a solution provided by Java Itself.
If you are using J2SE 6.0+ there is an in built solution provided along with JDK itself and inorder to go ahead with solution the below are set of API which you;d be using it for compiling Java Programs (Files)
http://java.sun.com/javase/6/docs/api/javax/tools/package-summary.html
How do i do that ??
Check out the below articles which would help you of how to do that
http://www.ibm.com/developerworks/java/library/j-jcomp/index.html
http://www.javabeat.net/javabeat/java6/articles/java_6_0_compiler_api_1.php
http://books.google.com/books?id=WVbpv8SQpkEC&pg=PA155&lpg=PA155&dq=%22javax+tools%22+compiling+java+file&source=web&ots=XOt0siYe-f&sig=HH27ovuwvJgklIf8omTykUmy-eM
Now once we are done with compilation.In order to run a Specific class all you ought to do is create an object and its specific methods of a specified class included in the CLASSPATH which you can manage it easily by usage little bit reflections.
Hope that might help :)
REGARDS,
RaHuL -
Not able to execute one Java program from another one via. exec()
Hi,
I am new to this forum, so I might be asking a very trivial quetion.
My program Ap1 is :
class Ap1
public static void main(String a[]) throws Exception
Runtime rt=Runtime.getRuntime();
Process p=null;
p=rt.exec("javac,Pr.java");
Runtime.getRuntime().exec("javac,Pr.java");
When I run the above program, I get below error at the line 7(bold one):
D:\batchwe>javac Ap1.java
D:\batchwe>java Ap1
Exception in thread "main" java.io.IOException: CreateProcess: javac,Pr.java err
or=2
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:429)
at java.lang.Runtime.exec(Runtime.java:326)
at Ap1.main(Ap1.java:7)
Path is set as below(which seems correct):
D:\batchwe>path
PATH=C:\Program Files\Java\jdk1.5.0_05\bin
I am just wondering how to call one Java program from another one?!
Please help.
thanks
RashmiHi diptaPB,
Thanks for your suggestion.
Here is how I tried it again, but not getting the desired output:
Class Pr looks like below -->
class Pr
public static void main(String a[])
System.out.println("hi from process");
When I run 'Pr' from command prompt, i get the desired output:
D:\batchwe>java Pr
hi from process
However, when I call 'Pr' from another program Ap2.java, it seems that it does not call 'Pr':
class Ap2
public static void main(String a[]) throws Exception
Runtime rt=Runtime.getRuntime();
Process p=null;
String[] cmd=new String[2];
cmd[0]="javac";
cmd[1]="Pr.java";
p=rt.exec("cmd");
D:\batchwe>javac Ap2.java
D:\batchwe>java Ap2
************* no output appears here***********************
Looking forward for your help.
regards
Rashmi -
Make a Java program call another program??
Is there a way to make a Java program execute another program?
Why dont we demonstrate:
Program (Windows-based) Calculator.exe
Code:
try {
Runtime.getRuntime().exec("c:/windows/calc.exe");
} catch(IOException e) {} -
Calling java program from PL/SQL code
Dear,
How to develop and call a java program from PL/SQL code?
What about if this program will call other java programs?Perhaps the Java Developer's Guide would be a good place to start
http://download.oracle.com/docs/cd/B19306_01/java.102/b14187/toc.htm
Justin -
Compiling one java file within another java program
Hi all,
I want to compile one java file say one.java within a java program say second.java.. i simply have no idea as how to proceed ..pls help!!http://onesearch.sun.com/search/onesearch/index.jsp?qt=dynamically+compile&subCat=siteforumid%3Ajava31&site=dev&dftab=siteforumid%3Ajava31&chooseCat=javaall&col=developer-forums
Just to give you an idea. -
Trying to compile a .java file from another .java file
Hello,
I'm trying to compile a .java file from another .java file using Runtime.exec...
String c[]=new String[3];
c[0]="cmd.exe"; c[1]="/c" ; c[2]="javac Hello.java";
Process p=Runtime.exec(c);
If anyone can help me in atleast getting the command prompt when Runtime.exec("cmd.exe") is executed...that would be great...I tried out notepad.exe, calc, explorer instead of cmd.exe...all the commands display their respective windows..except cmd.exe...the command prompt doesnt appear...
Please help me ASAP....
Thanks for your help in advance...
Regards.
AKhila.try this. ur code will be compliled and will get .class file. but console won't appear. is it a must for u?
public class Exec{
public static void main(String a[]) throws Exception{
String c[]=new String[3];
c[0]="cmd.exe"; c[1]="/c" ; c[2]="javac Hello.java";
Process p=Runtime.getRuntime().exec(c);
// or Runtime.getRuntime().exec("javac Hello.java"); -
How to pass a "object" as a prameter from one java class to another java
hi experts, I want to know "How to pass and get object as a parameter from one java class to another java class". I tried follwoing code just check it and give suggetions..
import Budget.src.qrybean;
public class ConfirmBillPDF extends HttpServlet
qrybean db = new qrybean();
SimplePDFTable pdfTable = new SimplePDFTable();
pdfTable.simplePDFTableShow("2010","2011","1","2","1","131","102");
}Here i want to pass db with simplePDFTableShow method. simplePDFTableShow is in another java class. So how can i do this.
And also i want to know, how this obj will get.
please help me.
Edited by: andy_surya on Jul 14, 2010 7:51 AMHi andy_surya
what is this i am not understand
pdfTable.simplePDFTableShow("2010","2011","1","2","1","131","102");but i am try to solve your problem try this
qrybean db = new qrybean();
SimplePDFTable pdfTable = new SimplePDFTable();
pdfTable.simplePDFTableShow(db);and access like this in SimplePDFtable class update your method
simplePDFTable(qrybean tempDB)
// write your code
} -
How to call a Java class from another java class ??
Hi ..... can somebody plz tell me
How to call a Java Class from another Java Class assuming both in the same Package??
I want to call the entire Java Class (not any specific method only........I want all the functionalities of that class)
Please provide me some slotuions!!
Waiting for some fast replies!!
Regards
Smita MohantyHi Smita,
you just need to create an object of that class,thats it. Then you will be able to execute each and every method.
e.g.
you have developed A.java and B.java, both are in same package.
in implementaion of B.java
class B
A obj = new A();
//to access A's methods
A.method();
// to access A's variable
//either
A.variable= value.
//or
A.setvariable() or A.getvariable() -
Difference between a java program and a java class
Hi there,
What is the difference between a java program and a java class?HI,
A java class is an object - described in a .class file - which has been given properties and behaviours and which can be instantiated in a program.
A java program( application ) consists of at least one class (one of which must be declared public) and which executes its main() method ( of the public class ) when it is started.
Every program is a class but not every class is a program.
Examples:
A java program
// this simple program will print out the first command line argument you put
// in after calling : java ScreenWriter "arguments"
class ScreenWriter
public static void main( String [] args )
if( args.length > 0 )
System.out.println( args[0] );
A java class
//the Math class is already defined in the java.lang package
//and contains a constant for PI
//So I can include the Math class with the PI constant in my simple program
//and now I have a program that involves my ScreenWriter class and the Math class too.
class ScreenWriter
public static void main( String [] args )
if( args.length > 0 )
System.out.println( args[0] );
System.out.println( "The value of Pi is: " + Math.PI );
When you see package and import declarations at the start of a program they are there to ensure the compiler knows which classes are used in the program.
Hope that helps,
Terry -
How do i run a java program an another directory?
How do I run a java program that's in a different directory?
I have been doing this in the command line:
java "C:\Document and Settings\freeOn\Desktop\Java\Test\test"
and I get
Exception in thread "main" java.lang.NoClassDefFoundError:
C:\Document and Settings\freeOn\Desktop\Java\Test\test
I just thought there might be a quick way to do this and not
have to cd to the following dir evertime i want to run an app in
console.
The test.java file is this:
import java.io.*;
public class test {
public static void main(String args[]) {
System.out.println("Testing.....");Ok I looked in the java help and found the classpath, this makes it alittle easier.
java -cp C:\DOCUME~1\freeOn\Desktop\Java\Test\ test
At least i can run this in the run dialog which makes it easier thanks for you help kota balaji -
Calling a Java Program from another
Hi,
I am putting together a Java program which will launch other Java programs.
I was thinking of doing this by calling unix scripts from my java program by getting an instance of the Runtime object and then calling the exec method with the script name.
I am just wondering what is the best practice when doing this? i.e., dealing with timeout issues, and errors in the scripts etc.
I have read this article, which is quite good: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1
Is there any code available on the web that deals with all of the basic error scenarios?
Is it good practice to use a java program to call another java program? Or is there a better way of doing this?
The main reason I am using java is because I am most familiar with it and I need to connect to a database as well and do some manipulation of results returned.
Any thoughts or suggestions would be much appreciated.
Message was edited by:
LondonJavaDevThanks Prometheuzz
Is the main difference between your suggestion and
using the Runtime.getRuntime().exec(cmd) way of doing
things is that in the former both eill run as the
same process (Thread) but in the second scenario the
.getRuntime().exec(cmd) will start a separate new
thread?
Many ThanksCorrect. But you can create a sort of wrapper class for A which implements Runnable and stuff it in it's own thread. If you're dealing with Java code only, I advise you to create threads yourself instead of letting your OS handle them.
Here's a demo:
class Foo {
public static void main(String[] args) {
(new Thread(new ARunner())).start();
B.main(null);
class ARunner implements Runnable {
public void run() {
A.main(null);
class A {
public static void main(String[] args) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) { /* ... */ }
System.out.println("A");
class B {
public static void main(String[] args) {
System.out.println("B");
}Of course, you can create a wrapper for B as well.
Maybe you are looking for
-
How to create a form based on table using dynamic page?
Hi, I need to create a form using dynamic page. How do you pass values from the html form to a oracle procedure that will get executed on submission of the form ? I could not find any documents which shows how to do that. Can anyone please help me ou
-
I don't know why, but my DVD drive is there when I start the computer but if I use Paralells and then quit my dvd drive is no longer recognized by my computer. NOW It is disappearing after sleeping overnight !!! Everytime I want to use a CD or DVD I
-
I am to iphone, been an An$%*#d user for years and now am loving life on the iphone. However, my son also has an ipod. When he downloads his games to is ipod, they show up on my iphone. He had the ipod before I had an iphone, so I am assuming our acc
-
I have raised Purchase Order to Vendor A in INR & vendor A procures from Foreign vendor B . Vendor A will give customs duty to Vendor B. Now vendor A will sale me the goods overseas to avoid sales tax in India . He will pass on the customs duty to
-
FBCJ transaction - ignore TDS for vendor
Hi Experts, I am posting a payment to a vendor in FBCJ transaction. That vendor is liable for TDS. But while posting in cash journal I want to ignore the TDS amount. My query is how to ignore the TDS for this vendor while posting through FBCJ ? Kind