2017-04-21 127 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