Controlling OS Processes - Java Runtime.exec()
Launching an OS process from within your code is right there with JNI calls – it’s something you do half-knowing there’s a good chance you’re going to get some unexpected results and some really bad exceptions down the line. Even so, it’s a necessary evil. But processes have another nasty angle to them - they have a tendency to dangle. The problem with launching process from within Java code so far has been that is was hard to control a process once it was launched. To help us with this Java 8 introduces three new methods in the Process class - destroyForcibly - terminates a process with a much higher degree of success than before. isAlive tells if a process launched by your code is still alive. A new overload for waitFor() lets you specify the amount of time you want to wait for the process to finish. This returns whether the process exited successfully or timed-out in which case you might terminate it. Two good use-cases for these new methods are -...


Comments
Post a Comment