java.io.IOException:无法运行程序“mysql”:CreateProcess error = 2,系统找不到指定的文件

时间:2016-02-29 12:29:55

标签: java mysql

我的数据库恢复程序出了问题,这个错误隐藏了我的幸福:

  

java.io.IOException:无法运行程序“mysql”:CreateProcess error = 2,   系统找不到指定的文件

当我浏览并从此路径打开文件时,

要恢复的文件位于D:/Backup/backup.sql中,然后单击恢复按钮时出现错误。请帮我解决这个问题。 以下是我的JFileChooser代码,用于浏览文件位置。

browseButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent event){

     String recPath = "";
         JFileChooser fc = null;
        if (fc == null) {
            fc = new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            fc.setAcceptAllFileFilterUsed(false);
    }
    int returnVal = fc.showDialog(null, "Open");
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        recPath = file.getAbsolutePath();

        sourceField.setText(recPath);   


    }

}   

}

);


recoveryButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent event){

    try{

        String databaseName ="jdbc:mysql://localhost:3306/myDB";
        String userName     ="abc";
        String password     ="123";
        String source       = sourceField.getText();
        int processComplete;

        String[] executeCmd = new String[]{"mysql",databaseName, "--user=" + userName, "--password=" + password, "-e", "source"+source};

        //sava the command in a array
        Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);// execute the command

        processComplete = runtimeProcess.waitFor();// get the result to variable

        if(processComplete==1){
        JOptionPane.showMessageDialog(null, "Restore Failed");
        }

        else if(processComplete==0){

        JOptionPane.showMessageDialog(null, "Restore Completed");

        }

        }
        catch(Exception ex){

        JOptionPane.showMessageDialog(null,ex); 

        }

        }


}   


);

3 个答案:

答案 0 :(得分:1)

你应该添加路径到' mysql'进入' Path'变量或在代码中指定完整路径:

尝试

String[] executeCmd = new String[]{"\FULL PATH HERE\mysql",databaseName, "--user=" + userName, "--password=" + password, "-e", "source"+source};

而不是

String[] executeCmd = new String[]{"mysql",databaseName, "--user=" + userName, "--password=" + password, "-e", "source"+source};

答案 1 :(得分:0)

这个答案在2018/06/07年是正确的......

String[] executeCmd = new String[]{"\FULL PATH HERE\mysql",databaseName, "--user=" + userName, "--password=" + password, "-e", "source"+source};

一个例子是:

String[] restoreCmd = new String[] { "C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\mysql ", bd,"--user=" + usuario, "--password=" + password, "-e", "source " + pathToFile }

答案 2 :(得分:0)

您可以添加“ \ FULL PATH To MySQL” 例如:“ C:\ Program Files \ MySQL \ MySQL Server 5.7 \ bin” 到环境路径变量