Following is my code.I don't know why it is not working.After debugging it,I found that runtimeProcess return "java.lang.ProcessImpl@1afe17b" and processComplete returns 1.I think i am not able to construct command which i am passing to .exec.Please helpme.
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;
It might just be an artefact of the way that you wrote the Question, but this doesn't look correct:
"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump.exe"
You've got 5 spaces between Program
and Files
, where there is normally only 1. That could be enough to prevent the shell from finding the executable.
UPDATE
I think you need to find out what is being written to stderr. The simple way would be to redirect that to a different file. (I'm not sure what syntax you use to do that in Windows command language ...)
A straight forward method would be to first try out the command on command line and use a String as a parameter rather than a String[] array.
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);
Failing that, you can always use the following to figure out what is going on behind the scenes.
Runtime r = Runtime.getRuntime();
r.traceInstructions(true);
r.traceMethodCalls(true);
r.exec("your command")
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.