问题
单独执行一个shell命令执行正常,但是用
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("./xxx.sh", null, new File(workdir));
process.waitfor( );
却不行,而且不报任何错误,首先检查是否有执行权限,还不行看下面
解决
对输出流和错误输出流做处理
try {
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("java -classpath C:\\projects\\workspace\\testing\\bin test.TestOutput");
InputStream inputStream = process.getInputStream();
InputStreamReader isr = new InputStreamReader(inputStream);
InputStream errorStream = process.getErrorStream();
InputStreamReader esr = new InputStreamReader(errorStream);
BufferedReader nomalReader = new BufferedReader(isr)
BufferedReader errorReader = new BufferedReader(esr)
String line = null;
while (line = nomalReader.readLine()) != null) {
Loger.info(line);
}
while (line = errorReader .readLine()) != null) {
Loger.info(line);
}
} catch (IOException e) {
e.printStackTrace();
}
原因
导致该现象的一个可能的原因是,该可执行程序的标准输出比较多,而运行窗口的标准输出缓冲区不够大,所以在执行的时候及时的把缓冲区的内容取出来才能不阻塞命令的执行