Pregunta

How can I get process state, that is it running or completed.

I am creating new process through:

processBuilder();
¿Fue útil?

Solución

There is no direct method available that can provide you the state of the process if you use use the waitFor then your current thread will wait until the process terminates.
So you can use exitValue function wrapped in function something like this,

public static boolean isProcessRunning(Process process) 
{
    try 
    {
        process.exitValue();
        return false;
    } 
    catch(IllegalThreadStateException e) 
    {
        return true;
    }
}

And then you can continue doing other work and call above method to check the state as and when needed.

Otros consejos

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Process p = pb.start();

followed by

p.exitValue() //throws an IllegalThreadStateException - if the subprocess represented by                //this Process object has not yet terminated.

A shameless copy and paste job from the documentation, btw. Always a good place to start.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top