2017-04-21 180 views
0

我創建了下面的方法來執行Linux命令:方法來執行Linux命令失敗

public void executeGetLogs(){ 
    try{ 
     Runtime rt = Runtime.getRuntime(); 
     String [] commands = {helper.getPythonPath(), helper.getLogsMainScript(), helper.getLogsManagementUrl(), helper.getLogsManagementUrlPrefix(), helper.getLogsManagementUsername(), helper.getLogsManagementPassword(), helper.getLogsNet(), helper.getLogsStorageUrl(), helper.getLogStorageUsername(), helper.getLogStoragePassword()}; 
     Process proc = rt.exec(commands); 

     BufferedReader stdInput = new BufferedReader(new 
       InputStreamReader(proc.getInputStream())); 

     BufferedReader stdError = new BufferedReader(new 
       InputStreamReader(proc.getErrorStream())); 

     // read the output from the command 
     logger.debug("Standard output from execution of get_logs:\n"); 
     String s = null; 
     while ((s = stdInput.readLine()) != null) { 
      logger.debug(s); 
     } 

     // read any errors from the attempted command 
     logger.debug("Standard error from execution of get_logs (if any):\n"); 
     while ((s = stdError.readLine()) != null) { 
      logger.debug(s); 
     } 
    } 
    catch(IOException e){ 
     logger.debug("Execution exception: " + e); 
    } 
} 

的方法似乎開始正常工作,但隨後失敗。

調試顯示以下的輸出:

2017-04-21 12:27:42,391 DEBUG Standard output from execution of get_logs: 

2017-04-21 12:27:44,360 DEBUG 411 new files found 
2017-04-21 12:27:44,363 DEBUG Downloading files... 
2017-04-21 12:27:44,446 DEBUG Standard error from execution of get_logs (if any): 

什麼我希望看到的是

2017-04-21 12:27:44,360 DEBUG 411 new files found 
2017-04-21 12:27:44,363 DEBUG Downloading files... 
Downloaded 10 of 447 
Downloaded 20 of 447 
Downloaded 30 of 447 

依此類推,直到下載了447

447我也可以看到,沒有下載。

當我在終端中運行它時,我的命令運行。

Java中是否有可能導致它退出?有一件事是,它可能需要幾秒鐘時間來處理的10塊每一個有可能的

while ((s = stdInput.readLine()) != null) { 
    logger.debug(s); 
} 

只是看到了空,因爲stdInput尚未出現,因此退出循環?如果是的話我該如何解決這個問題?

回答

0

到底了。我需要等待過程結束。這可以用下面的代碼來完成:

proc.waitFor(); 

我的方法現在看起來是這樣的:

public void executeGetLogs(){ 
    try{ 
     Runtime rt = Runtime.getRuntime(); 
     String [] commands = {helper.getPythonPath(), helper.getLogsMainScript(), helper.getLogsManagementUrl(), helper.getLogsManagementUrlPrefix(), helper.getLogsManagementUsername(), helper.getLogsManagementPassword(), helper.getLogsNet(), helper.getLogsStorageUrl(), helper.getLogStorageUsername(), helper.getLogStoragePassword()}; 
     Process proc = rt.exec(commands); 

     BufferedReader stdInput = new BufferedReader(new 
      InputStreamReader(proc.getInputStream())); 

     try{ 
      proc.waitFor(); 
     } 
     catch(InterruptedException e){ 
      logger.debug("Interrupted Exception : " + e); 
     } 

     BufferedReader stdError = new BufferedReader(new 
      InputStreamReader(proc.getErrorStream())); 

     // read the output from the command 
     logger.debug("Standard output from execution of get_logs:\n"); 
     String s = null; 
     while ((s = stdInput.readLine()) != null) { 
      logger.debug(s); 
     } 

     // read any errors from the attempted command 
     logger.debug("Standard error from execution of get_logs (if any):\n"); 
     while ((s = stdError.readLine()) != null) { 
      logger.debug(s); 
     } 
    } 
    catch(IOException e){ 
     logger.debug("Execution exception: " + e); 
    } 
} 
0

在java中執行代碼的更好方法是ProcessBuilder。例如:

//Create the command. 
ProcessBuilder command = new ProcessBuilder(helper.getPythonPath(), helper.getLogsMainScript(), helper.getLogsManagementUrl(), helper.getLogsManagementUrlPrefix(), helper.getLogsManagementUsername(), helper.getLogsManagementPassword(), helper.getLogsNet(), helper.getLogsStorageUrl(), helper.getLogStorageUsername(), helper.getLogStoragePassword()); 
//Will show console output in java console. 
command .inheritIO(); 
//Launches de command. 
Process process= command .start(); 

希望它有幫助。

+0

我給一個去。謝謝 – runnerpaul

+0

我使用它時似乎遇到同樣的問題。 – runnerpaul

+0

好的,這是一個想法。處理輸出有一個計數(已下載10的447等)。我在想如果我讀完總數。在這種情況下,447並循環計數。 while(coint <447){keep processing;}。顯然需要解析,所以我可以得到總數和當前數量。我是否想着正確的路線。除非另有說明,否則我會仔細研究一下。 – runnerpaul