我需要避免在不同的文件路徑錯誤和存儲數據庫備份使用mysqldump以MySQL的備份
public class NewClass {
public static void main(String args[]) throws IOException, SQLException {
String dbName = "test";
String dbUser = "root";
String dbPass = "root";
try {
String executeCmd = "";
executeCmd = "mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r backup.sql";
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Backup taken successfully");
} else {
System.out.println("Could not take mysql backup");
}
} catch (InterruptedException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
ERROR:run: Exception in thread "main" java.io.IOException: Cannot run program "mysqldump": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042) 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 mypkg.NewClass.main(NewClass.java:27) 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.(ProcessImpl.java:288) at java.lang.ProcessImpl.start(ProcessImpl.java:133) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023) ... 4 more Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)
是你的OS路徑變量中的mysqldump? –
你的'%PATH%'或'$ PATH'上有'mysqldump'程序嗎? – mthmulders