一、问题说明:
ava后端执行cmd命令,windows上命令行能够成功,但是java使用exec执行,大部分时间都卡顿,无法执行完,也不报错。
二、阻塞原因
Java执行Runtime.getRuntime().exec()时,JVM会创建一个新的子进程去调用cmd窗口命令,同时该子进程与JVM建立三个连接管道:标准输入流(getInputStream)、标准输出流(getOutputStream)、标准错误流(getErrorStream)。
执行的cmd命令是对rtsp视频流截图,因为并没有参数的输入和得到结果,所以以为没有必要连同管道,虽然cmd窗口能够成功运行,但是运行的过程中会产生警告信息,因此得到的错误流迟迟得不到接受,就进行卡顿。
三、解决方法
主程序中执行命令,对错误流进行记录
try {
Process process4 = Runtime.getRuntime().exec(com);
StreamGobbler errorGobbler = new StreamGobbler(process4.getErrorStream(),"ERROR");
errorGobbler.start();
StreamGobbler outGobbler = new StreamGobbler(process4.getInputStream(), "STDOUT");
outGobbler.start();
int proc = process4.waitFor();
if(proc==0){
System.out.println("成功");
}else {
System.out.println("失败");
}
if(process4!=null){
process4.destroy();
}
}catch (IOException | InterruptedException e){
e.printStackTrace();
}
引入StreamGobbler类
package aiconnect;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
public class StreamGobbler extends Thread {
InputStream is;
String type;
OutputStream os;
public StreamGobbler(InputStream is, String type) {
this(is, type, null);
}
StreamGobbler(InputStream is, String type, OutputStream redirect) {
this.is = is;
this.type = type;
this.os = redirect;
}
public void run() {
InputStreamReader isr = null;
BufferedReader br = null;
PrintWriter pw = null;
try {
if (os != null)
pw = new PrintWriter(os);
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null) {
if (pw != null)
pw.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally{
try {
br.close();
isr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}