2013-03-24 35 views
0

我正在編寫一個運行命令行的代碼,使用默認的apache執行程序。 我找到了獲取退出代碼的方式,但我找不到獲取進程ID的方法。用java獲取進程ID默認執行程序

我的代碼是:

protected void runCommandLine(OutputStream stdOutStream, OutputStream stdErrStream, CommandLine commandLine) throws InnerException{ 
DefaultExecutor executor = new DefaultExecutor(); 
    PumpStreamHandler streamHandler = new PumpStreamHandler(stdOutStream, 
      stdErrStream); 
    executor.setStreamHandler(streamHandler); 
    Map<String, String> environment = createEnvironmentMap(); 
try { 
     returnValue = executor.execute(commandLine, environment); 
    } catch (ExecuteException e) { 
     // and so on... 
     } 
     returnValue = e.getExitValue(); 
     throw new InnerException("Execution problem: "+e.getMessage(),e); 
    } catch (IOException ioe) { 
     throw new InnerException("IO exception while running command line:" 
       + ioe.getMessage(),ioe); 
    } 
} 

我應該爲了得到的ProcessID做什麼?

+0

看看[這個](http://stackoverflow.com/questions/5174426/graceful-kill-of-apache-commons-exec-process)。 – 2013-03-24 16:50:05

+0

Im正在使用Windows ... – Rivi 2013-03-27 07:44:53

回答

2

無法使用apache-commons API檢索進程的PID(也不使用底層Java API)。

「最簡單」的事情可能是讓你的外部程序以這樣的方式執行,即程序本身以某種方式返回它產生的輸出中的PID。這樣你可以在你的Java應用程序中捕獲它。

這是一個恥辱Java不會導出PID。這是over a decade的功能請求。

+0

例如,這將導致PID成爲第一行:'/ bin/sh -c'echo $$ && exec program args'' – Hamy 2015-10-07 14:13:10