2015-10-12 53 views
0

以下代碼總是返回1.我不明白爲什麼。在java中執行mysql restore cmd runtimeProcess.waitFor()總是返回1

String executeCmd[] = {"mysql"," -u" + dbUser," -p" + dbPass," -A"," -D"+dbName ," < " + restorePath}; 

Process runtimeProcess = Runtime.getRuntime().exec(executeCmd); 
int processComplete = runtimeProcess.waitFor(); 

if (processComplete == 0) { 
    System.out.println("Successfully restored from SQL : " + s); 
} else { 
    System.out.println("Error at restoring"); 
} 
+0

你在Windows或Linux上運行? – Reimeus

+0

我在linux環境下。 – Bee

+0

輸入重定向(「<」)由shell(「bash」)處理,而不是由您的操作系統處理。 – dummy

回答

1

我已經開始試圖解決這個問題一個星期,我終於想通了。

問題是,上面的代碼中的<(流重定向)。在Java中執行進程時不能使用流重定向(在這種情況下,它不起作用)。

所以,你必須做到這一點,而不使用流重定向。這樣做的一個方法是:

mysql -u root -p**** --execute "SOURCE \\path\\to\\sql" 

嘗試:

String executeCmd[] = {"mysql"," -u "+dbUser," -p"+dbPass," -A"," -D"+dbName," --execute"," \"SOURCE "+restorePath+"\""}; 

或者乾脆:

String executeCmd = "mysql -u "+dbUser+" -p"+dbPass+" -A -D "+"dbName -e \"SOURCE "+restorePath+"\""; 
1

重定向<由命令shell解釋。嘗試

String executeCmd[] = 
     {"bash", "-c", "mysql", "-u", dbUser, "-p", dbPass, "-A","-D", dbName , "< ", restorePath}; 
+0

我認爲這些參數不應該串聯:''-u「+ dbUser' - >'」-u「,dbUser' – dummy

+0

@Bee yes'<'仍然存在,所以在這種情況下需要命令shell ... – Reimeus

+0

不適合我。我剛剛複製了你的線。 :( – Bee