<html><body><div dir="ltr">You need to drain those streams in two dedicated threats - or in some cases use the redirect feature to pipe it to the stdout of the parent (good option for automation scripting)</div><div id="ms-outlook-mobile-body-separator-line" dir="ltr"><br><div dir="ltr">I think without a helper ProcessBuilder is hard to use for such cases </div></div><div id="ms-outlook-mobile-signature"><div dir="ltr">-- </div><div dir="ltr"><a href="https://bernd.eckenfels.net">https://bernd.eckenfels.net</a></div></div><div id="mail-editor-reference-message-container" class="ms-outlook-mobile-reference-message"><hr style="display: inline-block; width: 98%;"><div id="divRplyFwdMsg" dir="ltr"><span style="font-family: Calibri, sans-serif;"><b>From:</b> core-libs-dev <core-libs-dev-retn@openjdk.org> on behalf of Cay Horstmann <cay.horstmann@gmail.com><br><b>Sent:</b> Tuesday, April 15, 2025 8:53 AM<br><b>To:</b> Ron Pressler <ron.pressler@oracle.com>; Remi Forax <forax@univ-mlv.fr><br><b>Cc:</b> core-libs-dev <core-libs-dev@openjdk.org>; David Alayachew <davidalayachew@gmail.com><br><b>Subject:</b> Re: [External] : Re: My experience using Java to do CLI Scripting</span><div style="font-family: Calibri, sans-serif;"> </div></div><br>
<br>Il 14/04/25 23:46, Ron Pressler ha scritto:
<br>>
<br>>
<br>>> On 14 Apr 2025, at 21:48, Remi Forax <forax@univ-mlv.fr> wrote:
<br>>>
<br>>>
<br>>> Hi Ron,
<br>>> i think you need to close the inputReader
<br>>>
<br>>> public static String output(String... args) {
<br>>> ProcessBuilder processBuilder = new ProcessBuilder(args);
<br>>> try {
<br>>> Process process = processBuilder.start();
<br>>> try (BufferedReader reader = process.inputReader()) {
<br>>> return reader.lines().collect(Collectors.joining("\n"));
<br>>> }
<br>>> } catch (IOException e) {
<br>>> throw new IOError(e);
<br>>> }
<br>>> }
<br>>
<br>>
<br>> When the process terminates, the InputStream underlying the pipe is closed (after being drained and the unread bytes served by a ByteArrayInputStream), which *I think* should be sufficient, but I could be wrong.
<br>>
<br>> — Ron
<br>
<br>Reading the process output into a string or string stream raises a number of worries.
<br>
<br>* Do I need to wait for the process to complete?
<br>* Do I need to close anything?
<br>* How exactly do I use getInputStream()/inputReader() with *extreme care*, as the API doc warns me?
<br>
<br>If it is safe to call
<br>
<br>builder.start().inputReader().lines()
<br>
<br>or
<br>
<br>builder.start().getInputStream().readAllBytes()
<br>
<br>without waiting or closing, then the API doc could say so. If not, then maybe a safe method to get the entire output would be appropriate for Process?
<br>
<br>
<br>--
<br>
<br>Cay S. Horstmann | https://horstmann.com
<br>
<br></div><div> </div></body></html>