ProcessBulder loads the process but doesn't start it

你离开我真会死。 提交于 2019-12-11 06:34:08

问题


I use ProcessBuilder to start a new process (child) form a java application (host). 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. 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? Many thanks in advance.


回答1:


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.

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




回答2:


@MadProgrammer is correct. 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);
            }
        }


来源:https://stackoverflow.com/questions/15303573/processbulder-loads-the-process-but-doesnt-start-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!