• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to run .java file from Runnable

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just eliminated unnecessary lines here.

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What "/c" stands for after ("cmd.exe","/c")?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the information given about /c when I do a cmd /?

 
Alex Teslin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get the same result. Does "Program Files" space could be a problem? That is where my jdk is.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Possibly.

You can abbreviate it c:\\progra~1\\...
 
Alex Teslin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Despite the subject line, this has nothing to do with Runnable or with threads. Moving to "Java in General (Intermediate.)"
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alex,
Try including this after you call exec.

 
Alex Teslin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this.


[ February 24, 2006: Message edited by: Keith Lynn ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also called p.waitFor() just after compilation.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic