2013-03-24 29 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