I'm trying to execute task with Runtime.getRuntime().exec.
As expected
I am getting "task started" in console. And the task gets executed and was completed.
Issue
Never got "task completed" printed.
Do you know what could be the reason? or How can I handle?
Code
System.out.println("task started");
Process process = Runtime.getRuntime().exec("cmd.exe /c task.bat task.job");
process.waitFor();
System.out.println("task completed");
Why is process.waitFor() not returning?
The
execcommand is returning a process-handle (like the PID).If the process has already terminated, you can query its exit-value with
process.exitValue()asint:otherwise if the process is still running:
Same for
waitFor():Since the blocking is not what you probably want, can set a timeout here, then return of
waitFor(long, TimeUnit)changes.So you can wrap in a try-catch block to get complete control: