How to add a timeout value when using Java’s Runtime.exec()?

If you’re using Java 8 or later you could simply use the new waitFor with timeout:

Process p = ...
if(!p.waitFor(1, TimeUnit.MINUTES)) {
    //timeout - kill the process. 
    p.destroy(); // consider using destroyForcibly instead
}

Leave a Comment