Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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();

It's the name that I give through a jFileChooser. So, with the jFileChooser, I managed to create a database, create a table but I don't seem to be able to create a backup. – bluesony Dec 28, 2013 at 22:50 How do I read it? To select the file I did: private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) { if(jFileChooserSave.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){ File fisier = jFileChooserSave.getSelectedFile(); – bluesony Dec 28, 2013 at 23:02

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:-

  • If using windows then the UAC permissions for the destined location can be the issue.
  • 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);
        // 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

    I changed the code with the one below, I don't receive the error anymore but still can't find the dump file: Process exec = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump ", new String[]{fisier.getName(), ">", fisier.getName()+".sql;"}); – bluesony Dec 29, 2013 at 0:12 Try executing the command like this Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c",youMySqlcommand}); – Sanket Parikh Dec 29, 2013 at 0:14 I tried: Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","\"C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump \""+fisier.getName()+" > D:\\"+fisier.getName()+".sql;"}); I receive the error:'"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump "fisier' is not recognized as an internal or external command, operable program or batch file. – bluesony Dec 29, 2013 at 0:39 In that case, I would recommend you store the complete mysqldump command in string variable, and then pass it into the exec method of getRuntime(). Do print the string variable value and try executing from the cmd without java to make sure that the generated command is properly working. – Sanket Parikh Dec 29, 2013 at 0:51

    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.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •