mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
fix(LocalRunnerClient): correctly capture stdout and stderr to avoid missing output in method exec
This commit is contained in:
@@ -114,29 +114,53 @@ public class LocalRunnerClient extends RunnerClient {
|
|||||||
|
|
||||||
private SystemExecResult exec(String... command) {
|
private SystemExecResult exec(String... command) {
|
||||||
SystemExecResult result = new SystemExecResult();
|
SystemExecResult result = new SystemExecResult();
|
||||||
|
List<String> output = new ArrayList<>();
|
||||||
List<String> resultList = new ArrayList<>();
|
List<String> error = new ArrayList<>();
|
||||||
result.setResultList(resultList);
|
|
||||||
StringBuilder s = new StringBuilder();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Process process = Runtime.getRuntime().exec(command);
|
Process process = new ProcessBuilder(command)
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
.redirectErrorStream(false) // 分开读
|
||||||
String line;
|
.start();
|
||||||
while ((line = reader.readLine()) != null) {
|
|
||||||
s.append(line);
|
Thread stdoutThread = new Thread(() -> {
|
||||||
resultList.add(line);
|
try (BufferedReader r = new BufferedReader(
|
||||||
}
|
new InputStreamReader(process.getInputStream()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = r.readLine()) != null) {
|
||||||
|
output.add(line);
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Thread stderrThread = new Thread(() -> {
|
||||||
|
try (BufferedReader r = new BufferedReader(
|
||||||
|
new InputStreamReader(process.getErrorStream()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = r.readLine()) != null) {
|
||||||
|
error.add(line);
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
stdoutThread.start();
|
||||||
|
stderrThread.start();
|
||||||
|
|
||||||
int exitCode = process.waitFor();
|
int exitCode = process.waitFor();
|
||||||
|
stdoutThread.join();
|
||||||
|
stderrThread.join();
|
||||||
|
|
||||||
result.setOk(exitCode == 0);
|
result.setOk(exitCode == 0);
|
||||||
result.setTotal(s.toString().isEmpty() ? "响应为空" : s.toString());
|
result.setResultList(output.isEmpty() ? error : output);
|
||||||
|
result.setTotal(String.join("\n",
|
||||||
|
output.isEmpty() ? error : output));
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
result.setOk(false);
|
result.setOk(false);
|
||||||
result.setTotal(e.getLocalizedMessage());
|
result.setTotal(e.getMessage());
|
||||||
}
|
|
||||||
if (result.getTotal().isEmpty()) {
|
|
||||||
result.setOk(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user