public static void doPackage(String path){
Date begin = new Date();
Process shellProcess = null;
String cmd = "cmd /c "+path;
BufferedReader shellErrorResultReader = null;
BufferedReader shellInfoResultReader = null;
try {
shellProcess = Runtime.getRuntime().exec(cmd);
shellErrorResultReader = new BufferedReader(new InputStreamReader(shellProcess.getErrorStream()));
shellInfoResultReader = new BufferedReader(new InputStreamReader(shellProcess.getInputStream()));
String infoLine;
while ((infoLine = shellInfoResultReader.readLine()) != null) {
}
String errorLine;
while ((errorLine = shellErrorResultReader.readLine()) != null) {
}
// 等待程序执行结束并输出状态
int exitCode = shellProcess.waitFor();
Date end = new Date();
System.out.println("脚本执行成功耗时:"+ DateUtil.between(begin,end, DateUnit.SECOND));
if (0 == exitCode) {
} else {
}
} catch (Exception e) {
} finally {
if (null != shellInfoResultReader) {
try {
shellInfoResultReader.close();
} catch (IOException e) {
}
}
if (null != shellErrorResultReader) {
try {
shellErrorResultReader.close();
} catch (IOException e) {
}
}
if (null != shellProcess) {
shellProcess.destroy();
}
}
}
java 调用cmd 命令执行bat文件
最新推荐文章于 2023-11-06 16:03:21 发布