/**
* 运行命令行
* @param cmd
* @return
*/
private static List<String> runExec(String cmd) {
ArrayList<String> output = new ArrayList<String>();
try {
logger.debug("cmd = '" + cmd + "'");
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String line = null;
logger.debug("This is java runExec output:");
logger.debug("<OUTPUT>");
while ((line = br.readLine()) != null) {
output.add(line);
logger.debug(line);
}
logger.debug("</OUTPUT>");
int exitVal = proc.waitFor();
logger.debug("Process exitValue: " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
return output;
}
在java中执行批处理或者exe可执行程序或shell脚本时使用Runtime的exec()方法
最新推荐文章于 2021-03-21 21:36:44 发布