在下面的java代码中,我看到了 process.waitFor()
似乎没有退出。我已经准备好了 processBuilder.redirectErrorStream(true);
这将帮助我用标准输出重定向错误,并读取 inputStream
方法 readProcessOutput
. 我认为当我们读取所有输出和错误流时,进程应该退出。但目前,它受到了冲击。我能知道怎么解决这个问题吗?另外,我使用的是将标准i/o设置为与当前java进程相同的i/o。
...
ProcessBuilder processBuilder = new ProcessBuilder().inheritIO().command("sh",
"test.sh");
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
List<String> results = readProcessOutput(process.getInputStream());
setExitCode(process.waitFor());
...
private List<String> readProcessOutput(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 1);
List<String> lines = new ArrayList<>();
String line;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
inputStream.close();
bufferedReader.close();
return lines;
}
public int getExitCode() {
return exitCode;
}
private void setExitCode(int exitCode) {
this.exitCode = exitCode;
}
暂无答案!
目前还没有任何答案,快来回答吧!