簡體   English   中英

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

[英]java.io.IOException:Cannot run program “mysql”:CreateProcess error=2, The system cannot find the file specified

我的數據庫恢復程序出了點問題,這個錯誤隱藏了我的快樂:

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); 

        }

        }


}   


);

您應該將 '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};

這個答案在 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 }

您可以將“\\FULL PATH To MySQL”添加到環境路徑變量中,例如:“C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM