简体   繁体   English

mysqldump从java运行时返回代码6,但同样的命令从命令行运行正常

[英]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: 当我通过Runtime.getRuntime从Java运行相同的命令时,我得到返回代码6.相同的命令在命令行中正常工作:

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. 对于这两个命令,当从java运行并且没有转储时,我得到返回代码6。 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: 故意输入错误密码时,我会在java中返回代码2,并在命令行中出现连接错误:

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? 为什么在java中运行相同的命令时得到(错误)返回代码6并在命令行中正常工作?

Later Edit: I try it from Windows. 稍后编辑:我从Windows尝试。

Runtime.exec is not a shell , so redirections with > and < won't work. Runtime.exec不是shell ,因此使用>和<的重定向将不起作用。 Currently the command is passing > to mysqldump , which interprets it as the name for the table you want to export. 目前该命令传递>mysqldump ,其解释为名称为要导出表。 (Hence return code 6, "illegal table".) (因此返回代码6,“非法表”。)

There are two solutions: 有两种解决方案:

  1. Run a shell. 运行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() . 使用Process.getInputStream()将命令的输出自己写入文件。

The mysqldump now supports --result-file= mysqldump现在支持--result-file=

So it would look like this. 所以它看起来像这样。

mysqldump --user=root --password= --host=localhost dbname --result-file=c:\temp\dumpfile.sql

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpsURLConnection.getInputStream 在 Eclipse 中工作正常,但在导出代码并从命令行运行后服务器返回响应代码 503 - HttpsURLConnection.getInputStream works fine in Eclipse but server returns response code 503 after code is exported and run from command line 如何从命令行运行 java 项目(在 eclipse 中正常工作,gradle) - How to run java project from command line (works fine in eclipse, gradle) Java应用程序可以从netbeans正常运行,但不能从命令行或.jar文件运行 - Java application works fine from netbeans but not the command line or a .jar file 如何在Java代码中运行mysqldump命令? - How to run mysqldump command in java code? 为什么Java Processbuilder不能在其自己的目录中运行PMCMD(informatica命令),但可以在命令提示符下正常运行 - Why PMCMD (informatica command) is not run by Java Processbuilder from its own directory while it works fine with command prompt JSONobject在intellij中有效,但从命令行运行时不起作用 - JSONobject works in intellij but not when run from command line Shell 命令从 Java 失败,但在手动运行时有效 - Shell command fails from java but works when run manually VS Code 调试无法解析导入语句中的类,但使用命令行可以正常工作 - VS Code debug can't resolve classes from import statements while it works fine using command line 如何在后台运行Java代码中的命令行? - How do I run command line from Java code in the background? 无法从命令行运行Java单元测试代码 - Unable to run Java unit test code from the command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM