views:

94

answers:

2

Hi

in a Java application I need to run an external console application. With the window's ones everything is OK:

try {
System.out.println("Running...");
    Runtime.getRuntime().exec("notepad.exe");
    System.out.println("End.");
}
catch(Exception e) {
    System.out.println(e.getMessage());
}

launches notepad successfully.

But if I put D:\\MyProg.exe or .bat or even cmd.exe (which is it PATH as notepad is) it does not work. Without any exeptions. Just:

Running...
End.

Can somebody help? Thanx.

A: 

It is because notepad placed in special folder and this folder exists in Path variable.

Run cmd using following line:

Runtime.getRuntime().exec("cmd.exe /c start");

Run other application:

Runtime.getRuntime().exec("cmd.exe /c start C:\\path\\to\\app.exe");
Bar
but cmd.exe is also in "PATH". but it does not work. Also i preicse the path "D:\\MyProg.exe" but it does not help.And where do i need to execute your "start" ? In java ???
Andrew
Yeah, i'd like to, but as i mentioned above Runtime.getRuntime().exec("your.bat"); does not work too
Andrew
it just does not launch the *.bats
Andrew
Check paths carefully. Make sure application **really** launches.
Bar
cmd is in PATH (checked by typing "cmd" anywhere); JAVA application passes by the instruction correctly: log is: "Running... End."; notepad also launches correctly. But the console applications do not...
Andrew
in case of forgetting the path (for example "MyProg.bat" instead of "D:\\MyProg.bat") JAVA throws exeption. But if everything is OK the exeption is not thrown but the .bat is not ececuted.
Andrew
If you are trying to launch console application, use: `cmd.exe /c start C:\\app.exe`.
Bar
Also tried. But due to "inlauchebility" of cmd.exe from JAVA it does nothing.
Andrew
Try using full path to `cmd` (something like `C:\\WINDOWS\\system32\\cmd.exe /c start`).
Bar
+1  A: 

First off, most likely Runtime.exec() is returning asynchronously, so just printing "end" will always work, since the exec call returns immediately, which is what you're seeing.

There's a bunch of other problems that could be showing up here. I think what is happening is that the programs you are calling might be outputting I/O on stdout that you are failing to read, or perhaps you need to wait for it to finish before exiting the java process. There's a great article on the various problems with Runtime.exec() you should probably read, it covers this and other problems.

wds