1

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.

Mike D
  • 4,938
  • 6
  • 43
  • 99
  • Possible duplicate of [Process is being paused until I close the program](https://stackoverflow.com/questions/44210123/process-is-being-paused-until-i-close-the-program) – msrd0 May 27 '17 at 12:10

2 Answers2

2

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

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks @MadProgrammer! Your are correct. It started to work when I consumed the InputStream of the child process. Something like this **bold**'process = processBuilder.start(); is = process.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String line; while (!stopProcess) { if (br.ready()) { line = br.readLine(); System.out.println(line); } }' – user2149982 Mar 11 '13 at 22:25
0

@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);
            }
        }