使用“cmd /c start”时,当遇到路径中含有空格时,系统会报错。
这个问题很是让人苦恼,在网上摸索了很久,终于找到了好的解决方法。
当路径中有空格时,不使用 cmd /c start D:\Program Files (x86)\Java 命令,而是使用 cmd /c start "" "D:\Program Files (x86)\Java" 在start和路径之间加上一对双引号,以及用另一对双引号把路径包起来。
public static void main(String[] args){
String common = "cmd /c start \"\" ";
String dirPath = "\"D:\\Program Files (x86)\\Java\"";
String cmd = common + dirPath;
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
}