Executing a Java Program from within a Java Program
I need to execute the following Java Program from withing another Java Program. The office toolbar command line is
D:\WINDOWS\system32\java.exe -cp E:\Development\Eclipse\UpdateServer\Classes -server -showversion UpdateServer
I can find no combination of ProcessBuilder commands, including those that include "Cmd.exe /c" that will make this program run from within another Java Program. All the examples I can find only show how to run Windows *.exe programs. I keep getting error 123 from ProcessBuilder.start(), but I can find no documentation for error 123.
[580 byte] By [
vze29tvda] at [2007-11-26 18:10:45]

do you mean run it in the same JVM? just invoke it's main method then, like any other method. there's nothing magic about main
You can also certainly use the Process to invoke it the same way as an .exe file.
Good idea. I could just make it a part of the current program. Never thought of that. Thanks muchly!!!
Why not use a Runtime.exec() ?
> Good idea. I could just make it a part of the> current program. Never thought of that. Thanks> muchly!!!HuH? Who suggested that? BTW ... do you plan on actually splitting out your promised gift?
I cannot find a set of ProcessBuilder API methods that will invoke the program as stated. This works sorta:
ProcessBuilder pb = new ProcessBuilder("D:\\WINDOWS\\system32\\java.exe", "UpdateServer");
pb.directory(new File("E:\\Development\\Eclipse\\UpdateServer\\Classes\\"));
Map<String, String> env = pb.environment();
env.put("CLASSPATH", "E:\\Development\\Eclipse\\UpdateServer\\Classes");
But it is not what I intended (Note no -server or -showversion options). Plus the program executes a few lines and then dies. A separate test program worked OK though.
I tried the original ProcessBuilder commands again:
ProcessBuilder pb = new ProcessBuilder("D:\\WINDOWS\\system32\\java.exe", "-cp E:\\Development\\Eclipse\\UpdateServer\\Classes, "-server", "-showversion", "UpdateServer");
pb.directory(new File("E:\\Development\\Eclipse\\UpdateServer\\Classes\\"));
I dumped the error stream, and java.exe, when it is invoked, is saying it does not recognize the -cp option. What? It is criminal that one cannot access the whole command that is being sent to CreateProcess.
> Why not use a Runtime.exec() ?Because it does not work; program will not start.
Assuming your code didn't get mangled by the forum (it's missing one "), it may be that your "-cp E:\\Develop.." argument is getting quoted as it has a space in it; try passing "-cp" and "E:\\Develop..." as two arguments.
Process myProcess = Runtime.getRuntime().exec("cmd /C start /min /Dc:\\temp runtc.bat"); ... Where runtc.bat has all your commands in it ... the java -cp E:\ ... and all that.Try it.~Bill
> Assuming your code didn't get mangled by the forum
> (it's missing one "), it may be that your "-cp
> E:\\Develop.." argument is getting quoted as it has a
> space in it; try passing "-cp" and "E:\\Develop..."
> as two arguments.
That worked; specifically the following tested OK:
ProcessBuilder pb = new ProcessBuilder("D:\\WINDOWS\\System32\\Java.exe", "-cp", "E:\\Development\\Eclipse\\UpdateServer\\Classes\\", "-server", "-showversion", "UpdateServer" );
pb.directory(new File("E:\\Development\\Eclipse\\UpdateServer\\Classes\\"));
try{
Process p = pb.start();
InputStream is = p.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null)
System.out.println(line);
p.waitFor();
}catch(IOException ioe){
}
*
*
*
I was sure I tried that exact same code before, and it did not work, but now it does, at least at the top level (when it is in main of a test program). I will have to wait to try it until later when it is buried deep in a subroutine.
> > Process myProcess = Runtime.getRuntime().exec(
> "cmd /C start /min
> /Dc:\\temp runtc.bat");
>
> ... Where runtc.bat has all your commands in it ...
> the java -cp E:\ ... and all that.
> ry it.
>
> ~Bill
This worked well, except that /min is no longer supported on WinXP and I don't think /D oes what you tnink it does. Al least it works in a test program when it is in main. Some of my own work does that. I will have to wait until later to see if it works when it is buried deep in a subroutine. Thanks.
> > > > Process myProcess = Runtime.getRuntime().exec(
> > "cmd /C start /min
> > /Dc:\\temp runtc.bat");
> >
> > ... Where runtc.bat has all your commands in it
> ...
> > the java -cp E:\ ... and all that.
> > ry it.
> >
> > ~Bill
>
> This worked well, except that /min is no longer
> supported on WinXP and I don't think /D oes what you
> tnink it does. Al least it works in a test program
> when it is in main. Some of my own work does that.
> I will have to wait until later to see if it works
> when it is buried deep in a subroutine. Thanks.
I am on WinXP and that command works just fine as/is ... providing of course that one has a valid runtc.bat file in c:\temp.
You stated twice now - in reply to both my post and the other:" ... when it is buried deep in a subroutine." ... What does this mean?