@@ -22,7 +22,7 @@ public final class ProcessHelper {
2222 * environment variable (defaults to 4).
2323 *
2424 * @param command the command to be executed
25- * @return the merged stdout/stderr of the command
25+ * @return the standard output of the command
2626 * @throws ProcessException either if:
2727 * <ul>
2828 * <li>the command was unsuccessful
@@ -35,30 +35,49 @@ public final class ProcessHelper {
3535 public static String executeCommand (final String ... command ) throws ProcessException , InterruptedException {
3636 SEMAPHORE .acquire ();
3737
38- try (var process = new ProcessBuilder (command ).redirectErrorStream ( true ). start ()) {
39- var output = new StringJoiner ("\n " );
40- var readerThread = Thread .ofVirtual ().start (() -> {
38+ try (var process = new ProcessBuilder (command ).start ()) {
39+ var standardOutput = new StringJoiner ("\n " );
40+ var outputThread = Thread .ofVirtual ().start (() -> {
4141 try (var reader = process .inputReader (UTF_8 )) {
42- reader .lines ().forEach (output ::add );
42+ reader .lines ().forEach (standardOutput ::add );
4343 } catch (IOException e ) {
4444 LOGGER .at (Level .ERROR ).setCause (e ).log ("Error while closing process output reader" );
4545 }
4646 });
4747
48+ var standardError = new StringJoiner ("\n " );
49+ var errorThread = Thread .ofVirtual ().start (() -> {
50+ try (var reader = process .errorReader (UTF_8 )) {
51+ reader .lines ().forEach (standardError ::add );
52+ } catch (IOException e ) {
53+ LOGGER .at (Level .ERROR ).setCause (e ).log ("Error while closing process error reader" );
54+ }
55+ });
56+
4857 var finished = process .waitFor (1 , TimeUnit .MINUTES );
4958 if (!finished ) {
5059 process .destroyForcibly ();
51- readerThread .join ();
52- throw new ProcessException ("The command {} timed out after 1m\n {}" , command [0 ], output .toString ());
60+ outputThread .join ();
61+ errorThread .join ();
62+ throw new ProcessException ("The command {} timed out after 1m\n Standard output: {}\n Standard error: {}" ,
63+ command [0 ],
64+ standardOutput .toString (),
65+ standardError .toString ());
5366 }
5467
55- readerThread .join ();
68+ outputThread .join ();
69+ errorThread .join ();
70+
5671 var exitCode = process .exitValue ();
5772 if (exitCode != 0 ) {
58- throw new ProcessException ("The command {} exited with code {}\n {}" , command [0 ], exitCode , output .toString ());
73+ throw new ProcessException ("The command {} exited with code {}\n Standard output: {}\n Standard error: {}" ,
74+ command [0 ],
75+ exitCode ,
76+ standardOutput .toString (),
77+ standardError .toString ());
5978 }
6079
61- return output .toString ();
80+ return standardOutput .toString ();
6281 } catch (IOException e ) {
6382 throw new ProcessException (e );
6483 } finally {
0 commit comments