2

I'm trying to take a backup of a mysql database using the following code.

    public boolean backupDB() {

    String executeCmd = "mysqldump -u root -p 1234 --add-drop-database -B test -r D:\\backup\\aaa.sql";
    Process runtimeProcess;
    try {
        runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
            System.out.println("Backup created successfully");
            return true;
        } else {
            System.out.println("Could not create the backup");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return false;
}

But it always gives me this error:

CreateProcess error=2, The system cannot find the file specified

How can I resolve this? Thanks

2 Answers 2

1

Modify PATH external variable to contain a path to mysqldump.exe or use absolute path to the file.

set PATH=%PATH%;%MYSQL_HOME%\bin 

where MYSQL_HOME is a variable that contains a path to MySQL server installation folder.

When use with absolute path. Note, that this path depends on your comp and shouldn't be use in production code.

String executeCmd = "C:\\Programs Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump.exe -u root -p 1234 --add-drop-database -B test -r D:\\backup\\aaa.sql";
Sign up to request clarification or add additional context in comments.

10 Comments

should I download the mysqldump.exe from somewhere?
How to use absolute path to the file?
I tried set PATH=%PATH%;%MYSQL_HOME%\bin but it doesn't work
To get it programmatically, I would load up the Program files and program files (x86), scan both of these folders for a folder named "MySQL" (or something similar), then within that folder, search for a folder that contains "MySQL Server" (regex: "/MySQL Server \d+\.\d+"), locate "bin" within the "MySQL Server ..." folder, then check of bin contains "mysqldump.exe". That method is a little brute-force and there might be an easier/more efficient method, but that's just an idea.
I don't know may be you need to read an installation guide, you can find it on mysql site. Better to set a HOME env variable and update PATH, so you can run any command from mysql bin from anywhere.
|
0

Before you use mysqldump please specify its path like this :

String executeCmd = "\""+mysqldump_path+"bin\\mysqldump.exe\" --host=localhost --port=3306 --user=root --password=pass database > \""+path+"\"";
 runtimeProcess = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c",executeCmd});

       int processComplete = runtimeProcess.waitFor();
       System.out.println(processComplete);

       if(processComplete == 0) {
           JOptionPane.showMessageDialog(null, "Backup Created Successfully !");
           System.out.println("Backup Created Successfully !");
       }
       else{
          System.out.println("Couldn't Create the backup !");
          JOptionPane.showMessageDialog(null, "Couldn't Create the backup !");
       }

if it Creates error and goes of abnormally then this a pic of code that u can add to see the errors :

InputStream errorStream = runtimeProcess.getErrorStream();
byte[] buffer = new byte[errorStream.available()];
errorStream.read(buffer);
String str = new String(buffer);
System.out.println(str);

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.