wood burning stoves*
The moose likes Java in General and the fly likes How to run .java file from Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to run .java file from Runnable" Watch "How to run .java file from Runnable" New topic
Author

How to run .java file from Runnable

Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
Hi, I am trying to run .java file/class using exec() method. First, when try to compile in the code below, i get 1 as an exit value. I also would like to run that file - any suggestions please?

Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
I have just eliminated unnecessary lines here.

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Have you tried this?

public static void main(String[] args) throws Exception
{
try {

Runtime rt = Runtime.getRuntime();

String[] cmd = {"cmd.exe","/c",full path to javac.exe,"c://Temp//MainTest.java"};

Process p = rt.exec(cmd);
int exitVal = p.waitFor();
System.out.println("Process exitValue: " + exitVal);
}
catch(IOException e) {
System.err.println("Error on exec() method");
e.printStackTrace();
}
}
Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
What "/c" stands for after ("cmd.exe","/c")?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is the information given about /c when I do a cmd /?

Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
I get the same result. Does "Program Files" space could be a problem? That is where my jdk is.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Possibly.

You can abbreviate it c:\\progra~1\\...
Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
Thanks Keith,

I tried both to abbreviate to progra~1 and also used "quotes" for that path - still no luck. It must something else.
If i had ixitValue 2 on Windows it would mean "file not found" error. But not sure about 1.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Despite the subject line, this has nothing to do with Runnable or with threads. Moving to "Java in General (Intermediate.)"


[Jess in Action][AskingGoodQuestions]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Alex,
Try including this after you call exec.

Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
Thanks Keith,

I have managed to compile now - the error massage was very helpful and i corrected it.

Now, i am trying to run it. First i am creating another String[] cmd2 with

{"cmd.exe","/c", "c://progra~1//Java//jdk1.5.0_05//bin//java.exe", "c://Temp//Test1//MainTest"};

I am envoking getRutime() again like this:
Runtime rt2 = Runtime.getRuntime();
p = rt2.exec(cmd2);

and error i am getting now is:

java.lang.NoClassDefFoundError: c://Temp//Test1//MainTest
Exception in thread "main"

Now, it seems that compiles OK and if it does why can not find a class file.
Or am i missing with my running code?

Thanks again
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You'll probably need to explicitly set the classpath as part of the command line.

For instance, if your .class file is in c:\\temp, then you could do something like



Also don't list the full path to the .class file. The CLASSPATH should contain that information.
[ February 24, 2006: Message edited by: Keith Lynn ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Alex Teslin:

{"cmd.exe","/c", "c://progra~1//Java//jdk1.5.0_05//bin//java.exe", "c://Temp//Test1//MainTest"};
...
java.lang.NoClassDefFoundError: c://Temp//Test1//MainTest



First of all, using the "cmd /c" is not useful here. I imagine that what helped the first time was specifying the full path to javac.exe -- perhaps it's not on your PATH. But just as you don't need to type "cmd /c javac..." at the command line, neither do you need to do it with Runtime.exec().

Second, the argument to java.exe is not the path to a file. It is the name of a class, including its package name. "C:\" is never an appropriate part of that argument to java.exe.

Third, the doubled forward slashes are both unnecessary and inappropriate.

Assuming MainTest is in the default package, you could do something like

rt2.exec("c:/progra~1/Java/jdk1.5.0_05/bin/java.exe -cp c:/Temp/Test1 MainTest");

or if it's in the package "Test1" then you'd want to do

rt2.exec("c:/progra~1/Java/jdk1.5.0_05/bin/java.exe -cp c:/Temp Test1.MainTest");
Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
Keith,

I have tried many different ways - still no luck.
I will put my code here and hopefully for last time now.



I will be greatfull if you let me know what is wrong with it
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Try this.


[ February 24, 2006: Message edited by: Keith Lynn ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Alex Teslin:

I will be greatfull if you let me know what is wrong with it


My message has some important stuff -- you may not have read it yet. Another thing that that your compilation will almost certainly not have finished when you start the second process. You need to call p.waitFor() after starting the compilation and wait for it to return before you proceed.
Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
I have eliminated Double slashes - it was wrong.
I also deleted cmd.exe and c/ - it works now without them.

But when i try not to pass the classpath of the MainTest to java.exe as an argument - it does no work. Well it does not work either way. But by passing as an argument it completes the try block. Without the argument after the compilation jumps to Exception.

Keith, with your code still would not work.
Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
I also called p.waitFor() just after compilation.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Alex,
Here is an example showing how you can do what you want. Remember that when you run a program through exec, the Process does not put its output on standard out nor does it put its errors on standard err. You have to read those streams.

Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
Thank you for your example Keith,
The problem i had was precisely that, that I was expecting to get printouts on standard output not knowing that the program was running.

Thanks for your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to run .java file from Runnable
 
Similar Threads
Calling an exe from java
Running Java app with exec() method
Running unix command using java shows error
rmi when i running client and server that is ondifferent machine
run dos cammand in java code