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

I am trying to dump a MySQL database within my Java application the following way:

String[] command = new String[] {"cmd.exe", "/c", "C:/mysql/mysqldump.exe" --quick --lock-tables --user=\"root\" --password=\"mypwd\" mydatabase > \"C:/mydump.sql\""};
Process process = Runtime.getRuntime().exec(command);
int exitcode = process.waitFor();

The process fails with exit-code 6. I somewhere read that the operand ">" is not correctly interpreted and there was the hint to use "cmd.exe /c" as prefix. But it still doesn't work.

Any ideas?

You copy/pasted to much of the command I think. The command is piping output to a file. If you invoke this through Java then you don't do that, you capture the output of the process and do the writing to the file in the Java code. – Gimby Jul 22, 2014 at 12:53 The command by itself is correct. But you're right, that piping is somehow not supported by the Runtime library. I'll try to write the process output to the file by hand. Thanks so far! – salocinx Jul 22, 2014 at 12:59 Btw: piping resp. re-direction works with Runtime.exec() on Linux and I think also on MacOS. – salocinx Jul 22, 2014 at 16:12

Yes, you are right , some days ago I made class for exporting DataBase from MySQL... You coud read output sream from console and then write to file

    String[] command = new String[] {"cmd.exe", "/c", "\"C:/Program Files/MySQL/MySQL Server 5.6/bin/mysqldump.exe\" --quick --lock-tables --user=\"root\" --password=\"mypwd\" mydatabase "};
    Process process = Runtime.getRuntime().exec(command);
    BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
    while ((line = input.readLine()) != null) {
        System.out.println(line); //there you can write file 
    input.close();

Best Regards

Okay here's the final solution. You need to put the "process-reader to file-writer" code into a separate thread and finally wait for the process object to be finished:

    // define backup file
    File fbackup = new File("C:/backup.sql");
    // execute mysqldump command
    String[] command = new String[] {"cmd.exe", "/c", "C:/path/to/mysqldump.exe --quick --lock-tables --user=myuser --password=mypwd mydatabase"};
    final Process process = Runtime.getRuntime().exec(command);
    // write process output line by line to file
    if(process!=null) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                    try(BufferedReader reader = new BufferedReader(new InputStreamReader(new DataInputStream(process.getInputStream()))); 
                        BufferedWriter writer = new BufferedWriter(new FileWriter(fbackup))) {
                        String line;
                        while((line=reader.readLine())!=null)   {
                            writer.write(line);
                            writer.newLine();
                } catch(Exception ex){
                    // handle or log exception ...
        }).start();
    if(process!=null && process.waitFor()==0) {
        // success ...
    } else {
        // failed

On Linux you can directly re-direct the output of the command to a file by using ">" as usual... (and also on Mac OS X I think). So no need for the thread. Generally, please avoid white spaces in your path to the mysqldump/mysqldump.exe file!

you forgot to append ".exe" after mysqldump. if still not working, System.out.println(command) and try to execute in the windows terminal. moreover i would highly recommend to install mysqldump to a location without whitespaces in the path! this always leads to problems in windows. – salocinx Nov 10, 2015 at 23:14 insert Sytem.out.println(command[2]); after the line String[] command = ... and copy/paste this manually to your windows shell for testing the command. when you managed to work it there, copy it back to your java program. dont forget to add .exe and reinstall mysqldump.exe to a location without white spaces. let me know if it worked. i will be back in 8 hrs, i need a sleep. – salocinx Nov 10, 2015 at 23:24 process.waitFor() returns the exit-code of the process. insert System.out.println(process.waitFor()); before if(process!=null ...). if its !=0 there was an execution error => google for that exit code. – salocinx Nov 10, 2015 at 23:28 then probably there's a problem with your path to mysqldump. did you add .exe? try to copy mysqldump.exe to C:/mysqldump.exe and adjust your command[] accordingly. white spaces in paths on windows are really the hell! – salocinx Nov 10, 2015 at 23:41

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.