-1

以下是我的代码。我不知道为什么它不起作用。调试后,我发现 runtimeProcess 返回“java.lang.ProcessImpl@1afe17b”并且 processComplete 返回 1。我想我无法构造我的命令我正在传递给 .exec。请帮帮我。

public static boolean backupDB(String Database, String Dbuser, String Password) throws    IOException, InterruptedException 
{
Process runtimeProcess;
try{
runtimeProcess = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C","C:\\Program     Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump.exe -u"+Dbuser+" -p"+Password+Database+"       >F:\\backup.sql"});
System.out.println(runtimeProcess);
int processComplete = runtimeProcess.waitFor();
System.out.println(processComplete);
if (processComplete == 0) {
System.out.println("Backup created successfully");
return true;
}
else{
System.out.println("Could not create the backup");
}
}catch (Exception ex)
{
ex.printStackTrace();
}
return false;
4

2 回答 2

0

一种直接的方法是首先在命令行上尝试命令并使用 String 作为参数而不是 String[] 数组。

String cmd = "C:\\Program     Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump.exe -u"+Dbuser+" -p"+Password+Database+"       >F:\\backup.sql"
Runtime.getRuntime().exec(cmd);

如果做不到这一点,您总是可以使用以下内容来找出幕后发生的事情。

Runtime r = Runtime.getRuntime();
r.traceInstructions(true);
r.traceMethodCalls(true);
r.exec("your command")
于 2013-07-06T08:35:56.963 回答
0

它可能只是您编写问题的方式的产物,但这看起来不正确:

  "C:\\Program     Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump.exe"

Program和之间有 5 个空格Files,通常只有 1 个。这足以阻止 shell 找到可执行文件。


更新

我认为您需要找出正在写入 stderr 的内容。简单的方法是将其重定向到不同的文件。(我不确定您在 Windows 命令语言中使用什么语法来执行此操作......)

于 2013-07-06T04:57:59.887 回答