[External] : Re: My experience using Java to do CLI Scripting

forax at univ-mlv.fr forax at univ-mlv.fr
Tue Apr 15 09:44:08 UTC 2025


----- Original Message -----
> From: "Ron Pressler" <ron.pressler at oracle.com>
> To: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "cay horstmann" <cay.horstmann at gmail.com>, "core-libs-dev" <core-libs-dev at openjdk.org>, "David Alayachew"
> <davidalayachew at gmail.com>
> Sent: Monday, April 14, 2025 11:46:25 PM
> Subject: Re: [External] : Re: My experience using Java to do CLI Scripting

>> On 14 Apr 2025, at 21:48, Remi Forax <forax at univ-mlv.fr> wrote:
>> 
>> 
>> Hi Ron,
>> i think you need to close the inputReader
>> 
>>  public static String output(String... args) {
>>    ProcessBuilder processBuilder = new ProcessBuilder(args);
>>    try {
>>      Process process = processBuilder.start();
>>      try (BufferedReader reader = process.inputReader()) {
>>        return reader.lines().collect(Collectors.joining("\n"));
>>      }
>>    } catch (IOException e) {
>>      throw new IOError(e);
>>    }
>>  }
> 
> 
> 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.

I more worried about the iterator used by lines() throwing an exception (IO or charset related) or joining() throwing an OOME because in that case the reader may not be closed.

> 
> — Ron

regards,
Rémi


More information about the core-libs-dev mailing list