从 java 运行时,mysqldump 返回代码 6,但相同的命令在命令行中工作正常 [英] mysqldump returns code 6 when run from java, but the same command works fine from command line

查看:53
本文介绍了从 java 运行时,mysqldump 返回代码 6,但相同的命令在命令行中工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过 Runtime.getRuntime 从 Java 运行相同的命令时,我得到返回代码 6.相同的命令在命令行中工作正常:

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

对于这 2 个命令,当从 java 运行并且没有转储时,我得到了返回代码 6.从命令行工作正常(我在本地环境中没有密码.)

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

当故意输入错误的密码时,我在 java 中得到返回码 2,在命令行中出现连接错误:

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

我找到的返回码这里:

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

在 java 中运行相同的命令时我怎么得到(错误)返回码 6 并且在命令行中工作正常?

How come I get (error) return code 6 when running the same command in java and works fine from command line?

稍后我在 Windows 上尝试过.

Later I try it from Windows.

推荐答案

Runtime.exec 不是 shell,所以使用 > 和 < 重定向不会工作.目前,该命令正在将 > 传递给 mysqldump,后者将其解释为要导出的表的名称.(因此返回代码 6,非法表".)

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

有两种解决方案:

  1. 运行一个 shell.使用此命令而不是您拥有的命令:

  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"

  • 使用 Process.getInputStream() 将命令的输出写入文件.

  • Write the output from the command to a file yourself, with Process.getInputStream().

    这篇关于从 java 运行时,mysqldump 返回代码 6,但相同的命令在命令行中工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆