当前位置: 首页 > 知识库问答 >
问题:

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

仲孙向明
2023-03-14

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

Java . io . io异常:无法运行程序“MySQL”:CreateProcess错误=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个答案

胡博艺
2023-03-14

您可以在环境路径变量中添加“\MySQL的完整路径”,例如:“C:\ProgramFiles\MySQL\MySQL Server 5.7\bin”

吴胜涝
2023-03-14

这个答案在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 }
古畅
2023-03-14

您应该将路径添加到“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};
 类似资料: