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();