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()
?
Asked
Active
Viewed 52 times
-3

BrainStorm.exe
- 1,565
- 3
- 23
- 40
-
1No, I just couldn't find this answer else where and thought others might benefit from what I have learned. – BrainStorm.exe Apr 05 '17 at 22:06
-
Why are you guys down voting this? Do I need to rephrase my question? – BrainStorm.exe Apr 05 '17 at 22:14
-
1Do you really need a Unix version of the same answer? – Andreas Apr 05 '17 at 22:16
-
I thought it would be confusing if I just said "The program doesn't exit after closing stdin", since many programs don't even read it, but most Unix utilities do. – BrainStorm.exe Apr 05 '17 at 22:18
-
2Now that I see your duplicate question tag, I see your point. I'm going to leave this here though so others can find that answer with the search terms I was using. – BrainStorm.exe Apr 05 '17 at 22:32
1 Answers
-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