2012-11-14 204 views
4

我試圖運行下面的代碼是創建一個備份我的數據庫,但它顯示了一些運行時錯誤。備份mysql數據庫的Java代碼

但是,我試圖運行在mysql外殼的的System.out.println()輸出部分,(這是我在給定的代碼已經註釋)和它的工作

它顯示io文件問題。 Plz有人幫助我。

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 
+1

你會得到什麼錯誤? – dan

+0

@dan謝謝丹我複製錯誤,你可以看看它,並說出了什麼問題! –

回答

5

請檢查您的全球PATH環境變量是否有<路徑到MySQL > \在其斌(做一個echo %PATH%看)。實際上,您應該能夠在Plain DOS提示符下輸入System.out.println()內容,並且應該能夠運行它。即使與

,如果它不工作,請嘗試更改代碼來執行類似下面

runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd }); 

這應該理想地解決問題。

UPDATE:

如果沒有它在PATH環境變量的代碼更改爲以下

String executeCmd = "<Path to MySQL>/bin/mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + path; 
+0

OMG非常感謝你它工作!非常感謝你的欣賞。 –

0

runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });

我想這一個,但它沒有工作,所以我

this runtimeProcess = Runtime.getRuntime().exec(executeCmd);

0取代了它

和它的工作

0

我解決了這個問題,通過充分路徑mysqldump.exe

您可以通過

Map<String, String> env = System.getenv(); 
final String LOCATION = env.get("MYSQLDUMP"); 

我設置系統變量這樣得到如此的環境變量:

  • 變量名稱:MYSQLDUMP
  • 值:D:\xampp\mysql\bin\mysqldump.exe

現在只需要執行下面這段代碼,

String executeCmd = LOCATION+" -u " + DBUSER + " --add-drop-database -B " + DBNAME + " -r " + PATH + ""+FILENAME 
Process runtimeProcess = = Runtime.getRuntime().exec(executeCmd); 

注意:如果你沒有爲MySQL服務器配置的密碼只是刪除從代碼中-p PASSWORD屬性。並且不要忘記在創建新的系統變量後重新啓動計算機。