1

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

        }

        }


}   


);

3 Answers 3

1

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};
Sign up to request clarification or add additional context in comments.

Comments

0

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 }

1 Comment

Use formatting tools to make your post more readable. Code block should look like code block. Use bold italics if needed.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.