-3

I create a Process object in Java (the program is html tidy if that is important), feed it some data via stdin (Process.getOutputStream()), and closed the stream, but when I call Process.waitFor() it never returns because the process doesn't exit. How do I fix this without calling Process.destroy()?

BrainStorm.exe
  • 1,565
  • 3
  • 23
  • 40

1 Answers1

-3

It is possible that tidy is waiting for you to consume the output before it exits. This is likely if you just feed the program a lot of data and it is returning a lot of data for you. The solution is to consume the output before waiting for the program to exit. This is how I am handling it (not full code):

    ProcessBuilder builder = new ProcessBuilder();
    //-*snip*-
    Process p = null;
    Throwable error = null;
    int returnCode = -1;
    String output = null;
    StringBuilder stdErr = new StringBuilder(200);
    try{
        p = builder.start();
        try (Writer w = new BufferedWriter(new OutputStreamWriter(p.
                getOutputStream()))){
            w.write(html);
        }
        Process reference = p;
        //read both outputs so tidy has no reason to wait
        Thread errReader = new Thread(()->{
            StringBuilderWriter writer = new StringBuilderWriter(stdErr);
            try {
                IOUtils.copy(reference.getErrorStream(), writer,
                        Charsets.UTF_8);
            }
            catch (IOException ex) {
                //nom...
                //An IOE that happens here will likely happen when reading
                //stdout as well.
            }
        });
        errReader.start();
        //tidy might be waiting for us to consume output
        output = IOUtils.toString(p.getInputStream());
        returnCode = p.waitFor();
        errReader.join();
    }
BrainStorm.exe
  • 1,565
  • 3
  • 23
  • 40
  • Since you want to keep question for others to find, as a stepping stone to duplicate question, you can recover some rep by deleting the answer to clear the negative rep it's giving you. – Andreas Apr 05 '17 at 23:05
  • I could, but the answer might be useful to someone and it isn't that much rep. I'd rather help others than have a wealth of rep. – BrainStorm.exe Apr 06 '17 at 19:02