1

I'm executing mysqldump command from a Java class but I keep getting the error mentioned in the title.

This is the code I'm using:

        Process runtimeProcess = Runtime.getRuntime().exec("mysqldump -uroot -pmypassword valo > /etc/valbu.sql");
        int processComplete = runtimeProcess.waitFor();
        if(processComplete == 0){
            System.out.println("Backup taken successfully");
        } else {
            InputStream stderr = runtimeProcess.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            System.out.println("<ERROR>");
            while ( (line = br.readLine()) != null)
                System.out.println(line);
            System.out.println("</ERROR>");
            System.out.println("Could not take mysql backup");
        }

It looks like he doesn't understand that > isn't a table but a command to dump.

jlordo
  • 37,490
  • 6
  • 58
  • 83
Memme
  • 178
  • 11
  • Don't pass credentials as command line parameters! They might be visible for other users on the host (through `ps -o args`). Instead, pass them as environment variables, or through stdin. –  Jan 07 '13 at 11:05

3 Answers3

3

Common problem - You can't execute a statement with a pipe or a redirection. They can only be executed by the shell.

Further further reading

Community
  • 1
  • 1
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
3

Try using the result-file option in the mysqldump command instead of the redirection operator >:

mysqldump -uroot -pmypassword valo --result-file=/etc/valbu.sql

CfSimplicity
  • 2,338
  • 15
  • 17
1

This will not work because Runtime.exec() will not invoke a shell, and > is a shell redirection.

So, either use an array like { "/bin/bash", "-c", "thescripthere" }, (not recommended) or use, say, a ProcessBuilder.

fge
  • 119,121
  • 33
  • 254
  • 329
  • Thanks a lot, the array command works fine. Since it is not reccomended, i'd like to get into the processbuilder. I'll have some research about it thanks a lot! – Memme Jan 07 '13 at 11:29
  • I am facing the same problem, can some explain why is this approach not recommended ? – juancho Apr 18 '21 at 13:19