ch.vorburger.exec

直接使用原始 java.lang.ProcessBuilder API 从 Java 启动外部进程可能有点麻烦。在 Apache 的百科全书 Exec 的库使得它更容易一些。该 ch.vorburger.exec 库进一步扩大在下议院 Exec 能够使之真正方便:

 ManagedProcess proc = new ManagedProcessBuilder("path-to-your-executable-binary")
     .addArgument("arg1")
     .addArgument("arg2")
     .setWorkingDirectory(new File("/tmp"))
     .setDestroyOnShutdown(true)
     .setConsoleBufferMaxLines(7000)
     .build();

proc.start();
int status = proc.waitForExit();
int status = proc.waitForExitMaxMsOrDestroy(3000);
String output = proc.getConsole();

proc.startAndWaitForConsoleMessageMaxMs("started!", 7000);
// use service offered by external process...
proc.destroy();