NASORO
NASORO

Reputation: 11

java.io.IOException:Cannot run program "mysql":CreateProcess error=2, The system cannot find the file specified

Something is going wrong with my program for database recovery, this error hides my happiness:

java.io.IOException: Cannot run program "mysql":CreateProcess error=2, The system cannot find the file specified

file to be recovered is located in D:/Backup/backup.sql when I browse and open the file from this path then error appears when I click recovery button. Please help me solve this problem. below is my code with JFileChooser for browsing file location.

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

        }

        }


}   


);

Upvotes: 1

Views: 4742

Answers (3)

user13628926
user13628926

Reputation: 1

You can add "\FULL PATH To MySQL" eg : "C:\Program Files\MySQL\MySQL Server 5.7\bin" to the environment path variables

Upvotes: 0

user8242629
user8242629

Reputation:

This answer is correct in 2018/06/07...

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

A example will be :

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

Upvotes: 0

svichkar
svichkar

Reputation: 1856

You should add path to 'mysql' into 'Path' variables or specify full path in your code:

Try

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

instead of

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

Upvotes: 1

Related Questions