Java执行外部命令提示CreateProcess error=2, 系统找不到指定的文件的情况

问题描述:

Java执行外部命令提示CreateProcess error=2, 系统找不到指定的文件的情况,下面是我执行的命令

public class Demo27 {
    public static void main(String[] args) throws Exception {
        String[] cmds = new String[]{"D:\\mysql\\bin\\mysqldump.exe -u"};
        Process process = Runtime.getRuntime().exec(cmds);
        //等待这行执行完毕
        int exitValue = process.waitFor();
        System.out.println("执行结果:"+exitValue);
    }
}

解决:

Runtime.getRuntime().exec()执行的命令是连续的,不能带有空格,如果确实需要执行带有空格的命令,建议将命令写成脚本(window是bat脚本,Linux是xShell脚本),再去执行脚本的方式获取命令结果。

猜你喜欢

转载自blog.csdn.net/weixin_50003028/article/details/143330543