Exec class

V

Valerie

I am using the Exec class to execute dos commands within my program I
want to compile and run a java file, it works for the javac command
but not for the java command, my code;

try{
Exec.execWait("C:\\Program Files\\Java\\jdk1.5.0_01\\bin\\javac
C:\\JavaFiles\\Hello.java");
Exec.execWait("C:\\Program Files\\Java\\jdk1.5.0_01\\bin\\java
C:\\JavaFiles\\Hello");
}
catch (Exception e)
{ System.out.println(e);}

The error message;

====================================================================
Executing 'C:\Program Files\Java\jdk1.5.0_01\bin\javac
C:\JavaFiles\Hello.java'.


====================================================================
Executing 'C:\Program Files\Java\jdk1.5.0_01\bin\java
C:\JavaFiles\Hello'.

Error executing 'C:\Program Files\Java\jdk1.5.0_01\bin\java
C:\JavaFiles\Hello'.

Can anyone help? I am relatively new to java so any ideas are welcome.

Thanks.
 
K

klynn47

What I would do is create an array of commands

String[] cmd = {"cmd.exe","/c",...}

and then call exec and send it cmd
 
L

Lee Fesperman

Valerie said:
I am using the Exec class to execute dos commands within my program I
want to compile and run a java file, it works for the javac command
but not for the java command, my code;

try{
Exec.execWait("C:\\Program Files\\Java\\jdk1.5.0_01\\bin\\javac
C:\\JavaFiles\\Hello.java");
Exec.execWait("C:\\Program Files\\Java\\jdk1.5.0_01\\bin\\java
C:\\JavaFiles\\Hello");
}
catch (Exception e)
{ System.out.println(e);}

The error message;

====================================================================
Executing 'C:\Program Files\Java\jdk1.5.0_01\bin\javac
C:\JavaFiles\Hello.java'.

====================================================================
Executing 'C:\Program Files\Java\jdk1.5.0_01\bin\java
C:\JavaFiles\Hello'.

Error executing 'C:\Program Files\Java\jdk1.5.0_01\bin\java
C:\JavaFiles\Hello'.

Can anyone help? I am relatively new to java so any ideas are welcome.

The java command takes a class name not a path. If you need a path, you will need to
put it in the classpath. BTW, you should have just tried this at the command-line first.

Also, you should capture the error stream from the process, so you can see the actual
error output.

Newbie questions like this should be posted to comp.lang.java.help.
 
V

Valerie

Thank you. I tried it without the path initially and it did not work
so I decided to try it with the path as the documentation for the Exec
class suggests that it may help.. BTW the purpose of using the Exec
class is so that I can execute a command from within a java file not
on the command line (of course I checked that it ran from the command
line first!! (of course it worked from the command line!!).. when I
said I was new to java I did not mean that I knew absolutely nothing
about programming!
 
L

Lee Fesperman

Valerie said:
Thank you. I tried it without the path initially and it did not work
so I decided to try it with the path as the documentation for the Exec
class suggests that it may help.. BTW the purpose of using the Exec
class is so that I can execute a command from within a java file not
on the command line (of course I checked that it ran from the command
line first!! (of course it worked from the command line!!).. when I
said I was new to java I did not mean that I knew absolutely nothing
about programming!

Sorry, no disparagement intended; I meant a Java newbie.

I don't know the Exec class. Perhaps, it is new in 1.5. But, I do have some advice.

When you tried it from the command line, it only worked without the path, right? It
would be the same from exec.

Apparently, execWait() doesn't give you the error output. You should try a method that
allows you to snag syserr, then you can see what the actual failure message is. If the
Exec class doesn't give you one, try exec() in the Runtime class, use getErrorStream()
from the Process object you receive from exec() and display the actual failure message
you are receiving.

I would assume that is was a classpath problem. You may need to set the classpath when
you exec.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top