Reputation: 93
how can check Runtime.exec(cmd) is completed or not?
It is running but how can I check command is executed or not in java program?
If I used process to check then it is blocking current thread. If I stopped running program then Runtime.exec(cmd) is executing. What should I do?
Upvotes: 6
Views: 10142
Reputation: 2220
The Process
instance can help you manipulate the execution of your runtime through its InputStream and Outpustream.
The process instance has a waitFor()
method that allows the current Thread
to wait for the termination of the subprocess.
To control the completion of your process, you can also get the exitValue
of your runtime, but this depends on what the process returns.
Here is a simple example of how to use the waitFor
and get the process result (Note that this will block your current Thread until the process has ended).
public int runCommand(String command) throws IOException
{
int returnValue = -1;
try {
Process process = Runtime.getRuntime().exec( command );
process.waitFor();
returnValue = process.exitValue();
} catch (InterruptedException e) {
e.printStackTrace();
}
return returnValue;
}
Upvotes: 14