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: ">"
mysqldumpdirectly, so redirection will not apply.