|
22 | 22 | import com.google.gson.Gson; |
23 | 23 | import com.google.gson.annotations.SerializedName; |
24 | 24 | import java.io.BufferedReader; |
25 | | -import java.io.BufferedWriter; |
26 | 25 | import java.io.IOException; |
27 | 26 | import java.io.InputStream; |
28 | 27 | import java.io.InputStreamReader; |
29 | | -import java.io.OutputStream; |
30 | 28 | import java.io.OutputStreamWriter; |
31 | 29 | import java.nio.charset.StandardCharsets; |
32 | 30 | import java.nio.file.Path; |
@@ -56,17 +54,22 @@ public void execute(List<String> args, @Nullable String input) { |
56 | 54 | try { |
57 | 55 | List<String> command = new ArrayList<>(args); |
58 | 56 | command.add(0, javaExecutable.toString()); |
| 57 | + LOG.atDebug().addArgument(() -> String.join(" ", command)).log("Executing: {}"); |
59 | 58 | Process process = new ProcessBuilder(command).start(); |
60 | 59 | if (input != null) { |
61 | | - try (OutputStream stdin = process.getOutputStream(); |
62 | | - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin, StandardCharsets.UTF_8))) { |
63 | | - writer.write(input); |
| 60 | + try (var stdin = process.getOutputStream(); var osw = new OutputStreamWriter(stdin, StandardCharsets.UTF_8)) { |
| 61 | + osw.write(input); |
64 | 62 | } |
65 | 63 | } |
66 | | - new StreamGobbler(process.getInputStream(), this::tryParse).start(); |
67 | | - new StreamGobbler(process.getErrorStream(), stderr -> LOG.error("[stderr] {}", stderr)).start(); |
68 | | - if (process.waitFor() != 0) { |
69 | | - throw new IllegalStateException("Error returned by the Java command execution"); |
| 64 | + var stdoutConsummer = new StreamGobbler(process.getInputStream(), this::tryParse); |
| 65 | + var stdErrConsummer = new StreamGobbler(process.getErrorStream(), stderr -> LOG.error("[stderr] {}", stderr)); |
| 66 | + stdErrConsummer.start(); |
| 67 | + stdoutConsummer.start(); |
| 68 | + var exitCode = process.waitFor(); |
| 69 | + stdoutConsummer.join(); |
| 70 | + stdErrConsummer.join(); |
| 71 | + if (exitCode != 0) { |
| 72 | + throw new IllegalStateException("Error returned by the Java command execution: " + process.exitValue()); |
70 | 73 | } |
71 | 74 | } catch (IOException | InterruptedException e) { |
72 | 75 | Thread.currentThread().interrupt(); |
|
0 commit comments