I tried to run the following code which is to create a backup of my database but it shows some run time errors.
But, I tried to run the System.out.println() output part, (which I've commented in the given code) in mysql shell and it worked.
It shows io file problem. Plz somebody help me.
package files;
public class tableBackup_1 {
public boolean tbBackup(String dbName,String dbUserName, String dbPassword, String path) {
String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + path;
Process runtimeProcess;
try
{
System.out.println(executeCmd);//this out put works in mysql shell
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
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;
}
public static void main(String[] args){
tableBackup_1 bb = new tableBackup_1();
bb.tbBackup("test","harin","1234","C:/Users/Master/Downloads/123.sql");
}
}
mysqldump -u harin -p1234 --add-drop-database -B test -r C:/Users/Master/Downloads/123.sql
java.io.IOException: Cannot run program "mysqldump": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at files.tableBackup_1.tbBackup(tableBackup_1.java:12)
at files.tableBackup_1.main(tableBackup_1.java:34)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 5 more
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…