diff --git a/src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java b/src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java index c3db6e08e..80c2264b4 100644 --- a/src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java +++ b/src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java @@ -242,10 +242,23 @@ public static boolean externalCompile(List srcFiles, File outdir, File[] c StringBuilder errorBuffer = new StringBuilder(); StringBuilder outputBuffer = new StringBuilder(); - Thread out = copy(proc.getInputStream(), outputBuffer); - Thread err = copy(proc.getErrorStream(), errorBuffer); - - proc.waitFor(); + try { + // the compiler reads nothing from stdin - leaving the pipe open just + // holds a file descriptor until the Process is collected + proc.getOutputStream().close(); + + Thread out = copy(proc.getInputStream(), outputBuffer); + Thread err = copy(proc.getErrorStream(), errorBuffer); + + proc.waitFor(); + + // the readers can still be draining the pipes after the process exits, + // so join before reporting what they collected + out.join(); + err.join(); + } finally { + proc.destroy(); + } if (verbose || proc.exitValue() != 0) { if (outputBuffer.length() > 0) { @@ -347,9 +360,13 @@ private static File findJavaTool(String tool) { */ private static Thread copy(InputStream stream, final StringBuilder output) { final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, Charset.defaultCharset())); - Thread readerThread = new Thread(() -> - reader.lines().forEach(s -> output.append(s).append('\n')) - ); + Thread readerThread = new Thread(() -> { + try (BufferedReader r = reader) { + r.lines().forEach(s -> output.append(s).append('\n')); + } catch (IOException e) { + // nothing useful to do with a failure to drain the pipe + } + }); readerThread.start(); return readerThread; }