问题
When I run the same command from Java via Runtime.getRuntime I get the return code 6. The same command works fine from command line:
process = Runtime.getRuntime().exec(mysqldumpCommand);
int processComplete = process.waitFor();
For those 2 commands I get the return code 6 when run from java and no dump. The work fine from command line(I don't have a password on the local env.)
mysqldump --user=root --password= --host=localhost dbname > c:\temp\dumpfile.sql
mysqldump --user=root --password="" --host=localhost dbname > c:\temp\dumpfile.sql
When deliberately put wrong password I get return code 2 in java, and a connection error in command line:
mysqldump --user=root --password= --host=localhost dbname > c:\temp\dumpfile.sql
The return codes as I found them here:
Taken from client/mysqldump.c in MySQL 5.1.59:
#define EX_USAGE 1
#define EX_MYSQLERR 2
#define EX_CONSCHECK 3
#define EX_EOM 4
#define EX_EOF 5 /* ferror for output file was got */
#define EX_ILLEGAL_TABLE 6
How come I get (error) return code 6 when running the same command in java and works fine from command line?
Later Edit: I try it from Windows.
回答1:
Runtime.exec is not a shell, so redirections with > and < won't work. Currently the command is passing >
to mysqldump
, which interprets it as the name for the table you want to export. (Hence return code 6, "illegal table".)
There are two solutions:
Run a shell. Use this command instead of the one you have:
cmd.exe /c "mysqldump --user=root --password= --host=localhost dbname > c:\temp\dumpfile.sql"
Write the output from the command to a file yourself, with
Process.getInputStream()
.
回答2:
The mysqldump now supports --result-file=
So it would look like this.
mysqldump --user=root --password= --host=localhost dbname --result-file=c:\temp\dumpfile.sql
来源:https://stackoverflow.com/questions/15463990/mysqldump-returns-code-6-when-run-from-java-but-the-same-command-works-fine-fro