[英]Java execute shell commands, how to read output in real time?
Through the following code, I can execute and get the shell command, and get the output, but only after the end of the process, can I get the standard output.通过下面的代码,可以执行得到shell命令,得到output,但是只有在流程结束后,才能得到标准的Z78E6221F6393D1356681DB398F14CED6 How can I get the real-time output when the process is executing
进程执行时如何获取实时output
Process process = Runtime.getRuntime().exec(command);
// close process's output stream.
process.getOutputStream().close();
pIn = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(pin, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
this.buf.append(line + "\n");
}
process.waitfor();
you can use process builder to read response of command executed您可以使用流程构建器来读取执行命令的响应
ProcessBuilder pb = new ProcessBuilder();
pb.command(cmd);
Process proc = pb.start();
proc.waitFor();
InputStream inputStream = proc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String responseLine = "";
StringBuilder output = new StringBuilder();
while (( responseLine = reader.readLine())!= null) {
output.append(responseLine + "\n");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.