Redirect operator in MySQL causes error in Java

I was instructed to convert a PHP script that executes a shell command(mysqldump command) to Java. There were various resource available and executing the mysqldump command was a peice of cake. However, I wasn't getting the output that I expect. The output continously says that the mysqldump command has failed and would show an error

table ">" not found

hmmm... let's try to look at my command(this is just a sample)

mysqldump -uUser -pPassword mydatabase mytable > mydumpfile.sql

it works ok with the command line... But not with Java. I searched google and found numerous answers.

The most obvious way perhaps is not to use the redirect operator(>) and just create a File within the Java program and write the InputStream you get from class Process's getInputStream() into the a FileWriter object. But a more likely solution was the one I found where you just change the ">" to -r. I'm not sure what the problem really was but it seems that there were localization issues and any non-english machine would probably get this wrong if used inside Java. I use a properties file so changing the command was easy(and flexible). The command now looks like this

mysqldump -uUser -pPassword mydatabase mytable -r mydumpfile.sql

hope this helps! :)

P.S. The redirect operator is not a MySQL only command. It lives natively on Windows or other operating systems as well.
Published 12-20-2006 11:41 PM by lamia
Filed under: ,

Comments

Thursday, December 21, 2006 2:06 PM by cruizer

# re: Redirect operator in MySQL causes error in Java

well apparently the Java Process class that you used does not execute commands via the shell (command.com or cmd.exe on Win32 or the bash/csh/ksh shells on UNIX-like systems). it executes commands by running them directly. so in this case, it does not understand special command line characters like '<', '>' and '|'. what happened was that your > character was considered part of the command -- in this case, part of the table name as processed by the mysqldump utility.

Wednesday, December 27, 2006 4:01 PM by lamia

# re: Redirect operator in MySQL causes error in Java

Thanks! That makes a lot of sense!