I want to backup database in mysql 5.6.
For this I use this method:
public boolean backupDB(String dbName, String dbUserName, String dbPassword, String path) {
String executeCmd = "D://mysql-5.6.11-win32/bin/mysqldump -u " + dbUserName + " -p" + dbPassword
+ " --add-drop-database -B " + dbName + " -r " + path;
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(runtimeProcess.getErrorStream(), "ERR");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(runtimeProcess.getInputStream(), "OUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
int processComplete = runtimeProcess.waitFor();
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;
}
StreamGobbler is Thread that wraps runtimeProcess.getErrorStream()
and runtimeProcess.getInputStream()
into BufferedReader. BufferedReader merely reads line by line using readLine()
method. This is a hint I got on this article.
My problem is that method backupDB hangs on this line :
int processComplete = runtimeProcess.waitFor();
If I substitute waitFor method with exitValue I receive error
java.lang.IllegalThreadStateException: process has not exited
Therefore I must use waitFor method. And unfortunately it runs forever. I must press red icon on eclipse console to stop the JVM.
How to make process complete and backup mysql database? This is solved thanks!
EDIT To launch mysqldump.exe I need to write full path to it :
String executeCmd = "D://mysql-5.6.11-win32/bin/mysqldump -u "
Don't you know how to get rid of full path. I want to launch mysqldump independently on mysqldump.exe location on computer.
Maybe there is a way to get the path to mysqldump.exe having all connection details.
EDIT If I try to use mysqldump without full path to mysqldump.exe I receive this exception :
java.io.IOException: Cannot run program "mysqldump": CreateProcess error=2, Íå óäàåòñÿ íàéòè óêàçàííûé ôàéë
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at dbViewer.model.ConnectionManager.backupDB(ConnectionManager.java:273)
at dbViewer.model.ConnectionManager.main(ConnectionManager.java:337)
Caused by: java.io.IOException: CreateProcess error=2, Íå óäàåòñÿ íàéòè óêàçàííûé ôàéë
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:189)
at java.lang.ProcessImpl.start(ProcessImpl.java:133)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
... 5 more
Thanks!