I have the following code:
Process proc;
try
{
ProcessBuilder procBuilder = new ProcessBuilder(/* some args */);
proc = procBuilder.start();
if (proc.waitFor(30000, TimeUnit.MILLISECONDS))
{
//Etc...
}
else
{
//Handle it
}
}
catch (InterruptedException ie)
{
currentThread().interrupt();
}
finally
{
//What goes here?
}
I have tried to find some source that indicates whether it is required to call proc.destroy()
(should I check isAlive()
before calling destroy?), and to manually close its input/output/error streams, to no avail. Not even the official documentation makes this clear, from what I can tell.
Is it necessary, or even just good practice, to perform these operations when I am finished with the spawned process?
It's a good idea to call process.destroyForcibly()
in the finally block. waitFor
will not kill the process if it does not finish before the timeout, so in a situation where the spawned process hangs indefinitely, if you do not kill it then it could stick around forever.
Another use-case is suppose your process takes around 25 seconds to terminate normally. Suppose your thread is interrupted almost immediately after starting. It's probably better to kill the process rather than leave it running all that time, since the result will never be used anyway.
As for the streams, see Properly closing Java Process InputStream from getInputStream