可執行文件路徑如果包含空格,則在java中不能被獲取到。
此時Debug一下,會發現 project=null. project.waitFor 的返回值為1.但是去源路徑單擊bat文件是可以正常運行的,說明問題出在文件路徑上。
將文件路徑中的空格用雙引號引起來就可以了
原先的代碼
String batpath = file.getCanonicalPath() + "\\resources\\runTest.bat";
//run bat file
Process project = Runtime.getRuntime().exec("cmd.exe /c " + batpath.);
int exitcode=project.waitFor();
//kill the process
project.destroy();
logger.info(exitcode);
修改后的代碼
//run bat file
Process project = Runtime.getRuntime().exec("cmd.exe /c " + batpath.replaceAll(" ", "\" \""));
