?前言
兄弟篇:Java——通过Java代码启动批处理文件(一)
第一篇是直接执行批处理文件,但是有两种情况不能处理:
1.路径里面带有空格的情况,2.环境变量设置问题The CATALINA_HOME environment variable is not defined correctly;
本篇文章主要解决上述两个问题:
@Override
public void exeCmdInFolder(String folderPath, String batName) {
fileUtils.checkPath(folderPath);
log.info("Start to call cmd {} in {}", batName, folderPath);
StringBuilder sb = new StringBuilder();
try {
Process process = Runtime.getRuntime().exec("cmd.exe /c " + batName, null, new File(folderPath));
InputStream in = process.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line + "\n");
}
log.info(sb.toString());
in.close();
// process.waitFor();
} catch (Exception e) {
log.error("Faild to call cmd process cmd because {}", e.getMessage());
}
}
调用方式:
batService.exeCmdInFolder("批处理文件所在的文件夹", "批处理文件名称");
|