简体   繁体   English

ProcessBulder加载进程但不启动

[英]ProcessBulder loads the process but doesn't start it

I use ProcessBuilder to start a new process (child) form a java application (host). 我使用ProcessBuilder从Java应用程序(主机)启动新进程(子进程)。 Something like this: 像这样:

ProcessBuilder processBuilder = createProcess(commandLine);
processBuilder.directory(new File(baseDir));
processBuilder.redirectErrorStream(true);
Process process = null;
try {
    process = processBuilder.start();
} catch (Exception e) {
    e.printStackTrace();
}

I do see in the system monitor that the child process has been started but it's not functioning unless I stop the host application. 我确实在系统监视器中看到子进程已经启动,但是除非停止主机应用程序,否则子进程将无法运行。 More specifically the child proecess is a server and after starting it with a ProcessBuilder it's not responding to the requests if the host application still is running. 更具体地说,子进程是服务器,并且在使用ProcessBuilder启动它之后,如果主机应用程序仍在运行,则它不会响应请求。 Moreover, the port that server is using still is available. 而且,服务器正在使用的端口仍然可用。 The server starts working immediately if I stop the host application. 如果我停止主机应用程序,服务器将立即开始工作。 Is there anything that I missed or that's how ProcessBuilder suppose to work? 我有什么想念的吗?或者这就是ProcessBuilder的工作原理吗? Many thanks in advance. 提前谢谢了。

Under most circumstances, until a process's standard out buffer is emptied, it will not terminate. 在大多数情况下,除非清空进程的标准输出缓冲区,否则它不会终止。 It might be that your process has filled this buffer and has stopped (for some reason) 可能是您的进程已填满此缓冲区并已停止(出于某种原因)

Try consuming the processes standard out (via Process#getInputStream ) and see if that makes a difference. 尝试使用标准Process#getInputStream (通过Process#getInputStream ),看看是否有区别。

It could also be that the process is waiting for input for the user. 也可能是该进程正在等待用户输入。

Take a look at I'm not getting any output and probably the machine hangs with the code for an example 看看我没有得到任何输出,可能机器以示例的代码挂起

@MadProgrammer is correct. @MadProgrammer是正确的。 I was able to fix the issue and I want to answer to my question with a code example. 我能够解决此问题,并希望通过一个代码示例来回答我的问题。 That could be usefull for others too. 这对其他人也可能有用。 You need to consume the standard out of the child process after starting it. 启动后,您需要在子进程中使用该标准。 Something like this: 像这样:

        process = processBuilder.start();
        InputStream is = process.getInputStream();
        INputStreamReadr isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while (!stopProcess) {
            if (br.ready()) {
                line = br.readLine();
                System.out.println(line);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM