2013-07-19 87 views
2

我需要避免在不同的文件路徑錯誤和存儲數據庫備份使用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)

+0

是你的OS路徑變量中的mysqldump? –

+0

你的'%PATH%'或'$ PATH'上有'mysqldump'程序嗎? – mthmulders

回答

0

嘗試把完整路徑mysqldump程序

executeCmd = "/full/path/to/mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r backup.sql"; 

我會也會試圖將轉儲文件寫入的文件的完整路徑。

+0

Hi DaveHowes,this line error:executeCmd =「C:\\ Program Files \\ MySQL \\ MySQL Server 5.1 \\ bin \\ mysqldump -u」+ dbUser +「-p」+ dbPass +「」+ dbName +「 -r backup.sql「; – Kannan

+0

你有什麼錯誤? – DaveH

+0

嗨DaveHowes,你能看到這個鏈接我修改後的編碼http://stackoverflow.com/questions/17758634/store-database-backup – Kannan

0

你是否試圖給exec函數一個String數組?這在一段時間之前幫助了我。

String[] comm = new String[4]; 
comm[0] = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysqldump" 
comm[1] = "-u " + dbUser; 
comm[2] = "-p " + dbPass + " " + dbName; 
comm[3] = "-r backup.sql"; 
Process runtimeProcess = Runtime.getRuntime().exec(comm) 

因爲我使用一個String作爲參數時,有一些奇怪的行爲...

0

只需修改您的

executeCmd = "mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r backup.sql"; 

executeCmd = "mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName; 

後如果 - 添加else語句

String str=""; 

PrintWriter pw = new PrintWriter("path for this file/backup.sql"); 

Scanner outScanner = new Scanner(runtimeProcess.getInputStream()); 

while(outScanner.hasNextLine()){ 

pw.println(outScanner.nextLine()); 

} 

outScanner.close(); 

pw.close(); 
0

我有同樣的問題。對我來說,我只是在路徑變量中添加了mysql。舊的安裝被設置爲路徑變量。刪除它並添加新的路徑。