this question has answer here:
i create process
object in java (the program html tidy if important), feed data via stdin (process.getoutputstream()
), , closed stream, when call process.waitfor()
never returns because process doesn't exit. how fix without calling process.destroy()
?
it possible tidy
waiting consume output before exits. if feed program lot of data , returning lot of data you. solution consume output before waiting program exit. how handling (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 tidy has no reason 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 happens here happen when reading //stdout well. } }); errreader.start(); //tidy might waiting consume output output = ioutils.tostring(p.getinputstream()); returncode = p.waitfor(); errreader.join(); }
Comments
Post a Comment