Shaikh Arbaaz
Shaikh Arbaaz

Reputation: 155

Error in backup of MySQL from Java

I have developed a Java application and it's in last stage.

The problem is that I cannot execute the mysqldump with Runtime.getRuntime().exec().

try {
    String[] command = new String[] {
        "cmd.exe",
        "/c",
        "mysqldump --host=" + host + " --user=" + dbuser + " --password=" + dbpass + " " + dbname + " > " + filename
    };
    Process runtimeProcess = Runtime.getRuntime().exec(command);
    int ProcessComplete = runtimeProcess.waitFor();
    if (ProcessComplete == 0) {
        JOptionPane.showMessageDialog(null, "Database backup has been done successfully");
    }
    else {
        JOptionPane.showMessageDialog(null, "Database backup was unsuccessfull");
        JOptionPane.showMessageDialog(null, command);
    }
}
catch (IOException | InterruptedException exc) {
    Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, exc);
}

Can anyone help me with this?

Upvotes: 1

Views: 49

Answers (1)

Darshan Patel
Darshan Patel

Reputation: 2899

I tried your code in Windows and it works fine for me.

Open cmd, type mysqldump and hit enter.

If it is executed successfully, that means your code should work properly if your database configuration is correct.

If you get 'mysqldump' is not recognized as an internal or external command then either you have to set the MySQL path in your environment variable, or specify the full path for mysqldump in your Java code like this:

String[] command  = new String[] {
    "cmd.exe",
    "/c",
    "c://mysql/bin/mysqldump --host=" + host + " --user=" + dbuser + " --password=" + dbpass + " " + dbname + " > " + filename
};

Upvotes: 1

Related Questions