使用java apache默认执行器获取进程ID

使用java apache默认执行器获取进程ID,java,apache-commons-exec,Java,Apache Commons Exec,我正在编写一段代码,使用apache的默认执行器运行命令行。 我找到了获取退出代码的方法,但找不到获取进程ID的方法 我的代码是: protected void runCommandLine(OutputStream stdOutStream, OutputStream stdErrStream, CommandLine commandLine) throws InnerException{ DefaultExecutor executor = new DefaultExecutor();

我正在编写一段代码,使用apache的默认执行器运行命令行。 我找到了获取退出代码的方法,但找不到获取进程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);
    }
}
受保护的void runCommandLine(OutputStream stdOutStream、OutputStream stdErrStream、CommandLine CommandLine)引发InnerException{
DefaultExecutor executor=新的DefaultExecutor();
PumpStreamHandler streamHandler=新的PumpStreamHandler(stdOutStream,
stream);
执行人:setStreamHandler(streamHandler);
映射环境=createEnvironmentMap();
试一试{
returnValue=executor.execute(命令行、环境);
}捕获(执行异常e){
//等等。。。
}
returnValue=e.getExitValue();
抛出新的InnerException(“执行问题:+e.getMessage(),e);
}捕获(ioe异常ioe){
抛出新的InnerException(“运行命令行时发生IO异常:”
+ioe.getMessage(),ioe);
}
}

我应该怎么做才能获得ProcessID?

无法使用apache commons API(也不能使用底层API)检索进程的PID

“最简单”的事情可能是让您的外部程序以这样的方式执行,即程序本身在生成的输出中以某种方式返回其PID。这样,您就可以在java应用程序中捕获它


很遗憾java没有导出PID。这是一个功能要求

无法使用apache commons API(也无法使用底层API)检索进程的PID

“最简单”的事情可能是让您的外部程序以这样的方式执行,即程序本身在生成的输出中以某种方式返回其PID。这样,您就可以在java应用程序中捕获它


很遗憾java没有导出PID。这是一个功能要求

在Java 9及更高版本中,有一种方法可以检索流程对象的PID。然而,要在ApacheCommonsExec中访问流程实例,您需要使用一些非文档化的内部构件

下面是一段适用于Commons Exec 1.3的代码:

DefaultExecutor executor = new DefaultExecutor() {
    @Override
    protected Process launch(final CommandLine command, final Map<String, String> env, final File dir) throws IOException {
        Process process = super.launch(command, env, dir);
        long pid = process.pid();
        // Do stuff with the PID here... 
        return process;
    }
};
// Build an instance of CommandLine here
executor.execute(commandLine);
DefaultExecutor executor=新的DefaultExecutor(){
@凌驾
受保护的进程启动(最终命令行命令、最终映射环境、最终文件目录)引发IOException{
Process=super.launch(命令、环境、目录);
长pid=process.pid();
//在这里用PID做一些事情。。。
返回过程;
}
};
//在此处构建命令行实例
executor.execute(命令行);

在Java 9及更高版本中,有一种方法可以检索流程对象的PID。然而,要在ApacheCommonsExec中访问流程实例,您需要使用一些非文档化的内部构件

下面是一段适用于Commons Exec 1.3的代码:

DefaultExecutor executor = new DefaultExecutor() {
    @Override
    protected Process launch(final CommandLine command, final Map<String, String> env, final File dir) throws IOException {
        Process process = super.launch(command, env, dir);
        long pid = process.pid();
        // Do stuff with the PID here... 
        return process;
    }
};
// Build an instance of CommandLine here
executor.execute(commandLine);
DefaultExecutor executor=新的DefaultExecutor(){
@凌驾
受保护的进程启动(最终命令行命令、最终映射环境、最终文件目录)引发IOException{
Process=super.launch(命令、环境、目录);
长pid=process.pid();
//在这里用PID做一些事情。。。
返回过程;
}
};
//在此处构建命令行实例
executor.execute(命令行);

看一看。我在使用Windows…看一看。我在使用Windows…例如,这将导致PID成为打印的第一行:
/bin/sh-c'echo$$&exec program args'
例如,这将导致PID成为打印的第一行:
/bin/sh-c'echo$$&exec program args'