1

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?

3
  • 1
    Can't execute in what way? What happens? What do you expect to happen instead? Is there an exception? On which line? Commented Jul 25, 2015 at 12:10
  • i got the solution buddy....very long back...thanks for asking' Commented Sep 15, 2015 at 11:06
  • i just created a bat file and did it.. Commented Sep 15, 2015 at 11:07

1 Answer 1

1

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

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.