简体   繁体   中英

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

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.

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() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM