• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

方法来执行Linux命令失败

java 来源:runnerpaul 6次浏览

我创建了下面的方法来执行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尚未出现,因此退出循环?如果是的话我该如何解决这个问题?

===========解决方案如下:

到底了。我需要等待过程结束。这可以用下面的代码来完成:

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)