Question

Try to backup mysql DB from Java application (IDE Netbeans) using the following command but can't seem to find the file even though I specified a path:

Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;");

Also, I don't receive any errors, so I assumed that the backup has been done. How can I find the file? Regarding fisier.getName(): File fisier = jFileChooserSave.getSelectedFile();

Was it helpful?

Solution 2

You can test the below mentioned code for testing your mysqldump command output. There can be two main reasons to why the file is not creating, per my assumptions:-

  1. If using windows then the UAC permissions for the destined location can be the issue.
  2. You might be facing a syntax issue in the final mysqldump command generated to be executed by the java runtime.

     //Process exec = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;");
    
    Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;"});
    
    //Wait for the command to complete, and check if the exit value was 0 (success)
    if(exec.waitFor()==0)
    {
        //normally terminated, a way to read the output
        InputStream inputStream = exec.getInputStream();
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
    
        String str = new String(buffer);
        System.out.println(str);
    }
    else
    {
        // abnormally terminated, there was some problem
                    //a way to read the error during the execution of the command
        InputStream errorStream = exec.getErrorStream();
        byte[] buffer = new byte[errorStream.available()];
        errorStream.read(buffer);
    
        String str = new String(buffer);
        System.out.println(str);
    
    }
    

The redirection operator doesn't works when using Process exec = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;");

It is becasue it does not invokes the command shell, so we cannot get the functionality of the redirection operator, in order to fix this we can execute a command prompt (cmd) followed by the mysqldump command, and it will work.

Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;"});

Here /c in the cmd.exe specify that execute the passed command and terminate the process. The actual command when executed by java runtime will become

cmd.exe /c yourmysqldumpCommand

OTHER TIPS

For non-windows users:

String dump = "mysqldump -usome_user -psome_pass database_name   > path/to/file.sql";
String[] cmdarray = {"/bin/sh","-c", dump};
Process p = Runtime.getRuntime().exec(cmdarray);
if (p.waitFor() == 0) {
    // Everything went fine
} else {
   // Something went wrong
}

Using the cmd array is important. Otherwise exec cannot parse '>' and file name and you will get an error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top