Search code examples
javaprocessexecution

Java's Process.exec, is there a better way to handle it?


Easy question for the java experts out there:

Reading using Process.exec and http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4 it seems that the only way I can run a process from a java application, get its output (and error stream) and avoid my application from blocking forever, is to create two threads that will consume the stdout and stderr.

Maybe I have been spoiled by .net and python, but this seems like an enormous overkill to running a process. Any other, simpler way to do it?


Solution

  • Not really no. I would recommend Apache's Commons Exec library which deals with most of the nasty traps with using the Process API. I use it in production, no issues found.