2

I wanted to backup a MySQL database using ProcessBuilder and the > character is not being interpreted as I expected. This is my code:

java.util.List<String> cmd = new java.util.ArrayList<>();
cmd.add("mysqldump");
cmd.add("-u");
cmd.add("root");
cmd.add("-p"+password);
cmd.add("DBx");
cmd.add(">");
cmd.add("DBbk.sql");

ProcessBuilder pb = new ProcessBuilder(cmd);
pb.directory(new File("."));
Process p = pb.start();
p.waitFor();

BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while((line = err.readLine()) != null) {
    System.out.println(line);
}

The output is:

Warning: Using a password on the command line interface can be insecure.
mysqldump: Couldn't find table: ">"
1
  • 2
    Redirection is a shell function. You appear to be running mysqldump directly, so redirection will not apply. Commented Oct 4, 2013 at 3:17

2 Answers 2

7

The process builder is executing the program directly, not executing it through a shell/command prompt. Therefore, you don't get any nice features of the shell like the > character for redirection. Sorry.

Sign up to request clarification or add additional context in comments.

Comments

1

Give this a try (just displaying the significant portion of the solution):

new ProcessBuilder("cmd", "/C", "your_command > DBbk.sql").start() 

This way you're explicitly calling cmd. In Linux you could call bash or whatever shell you use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.