Reputation: 201
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.
Upvotes: 1
Views: 3662
Reputation: 32303
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.
Upvotes: 3
Reputation: 2146
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");
Upvotes: -1