1

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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");

9 Comments

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 ???
Yeah, i'd like to, but as i mentioned above Runtime.getRuntime().exec("your.bat"); does not work too
it just does not launch the *.bats
Check paths carefully. Make sure application really launches.
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...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.