2
votes

I need to avoid the error and store database backup in separate file path using mysqldump

public class NewClass {

    public static void main(String args[]) throws IOException, SQLException {
        String dbName = "test";
        String dbUser = "root";
        String dbPass = "root";
        try {
            String executeCmd = "";
            executeCmd = "mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r backup.sql";

            Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println("Backup taken successfully");
            } else {
                System.out.println("Could not take mysql backup");
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }    

    }
}

ERROR:run: Exception in thread "main" java.io.IOException: Cannot run program "mysqldump": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042) at java.lang.Runtime.exec(Runtime.java:615) at java.lang.Runtime.exec(Runtime.java:448) at java.lang.Runtime.exec(Runtime.java:345) at mypkg.NewClass.main(NewClass.java:27) Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(ProcessImpl.java:288) at java.lang.ProcessImpl.start(ProcessImpl.java:133) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023) ... 4 more Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

4
Is mysqldump in your OS path variable?Sam Aleksov
Is the mysqldump program on your %PATH% or $PATH?mthmulders

4 Answers

0
votes

Try putting the full path the mysqldump program

executeCmd = "/full/path/to/mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r backup.sql";

I'd also be tempted to put the full path the file where the dump is to be written as well.

0
votes

Have you tried to give the exec function a String-array? This helped me some time ago.

String[] comm = new String[4];
comm[0] = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysqldump"
comm[1] = "-u " + dbUser;
comm[2] = "-p " + dbPass + " " + dbName;
comm[3] = "-r backup.sql";
Process runtimeProcess = Runtime.getRuntime().exec(comm)

Because i had some strange behavior when using a single String as parameter ...

0
votes

Simply modify your

executeCmd = "mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r backup.sql";

to

executeCmd = "mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName;

and add after if-else statement

String str="";

PrintWriter pw = new PrintWriter("path for this file/backup.sql");

Scanner outScanner = new Scanner(runtimeProcess.getInputStream());

while(outScanner.hasNextLine()){

pw.println(outScanner.nextLine());

}

outScanner.close();

pw.close();
0
votes

I have the same problem. For me I just added the mysql in the path variable. An old installation was set in path variable. Removed it and add the new path.