mysqldump returns code 6 when run from java, but the same command works fine from command line

自古美人都是妖i 提交于 2019-12-23 20:14:48

问题


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:

  1. 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"
    
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!