执行Linux命令的方法失败

时间:2017-04-21 11:48:05

标签: java linux command

我创建了以下方法来执行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);
}

只看到一个null,因为stdInput尚未出现所以它退出循环?如果是这样,我该如何解决这个问题?

2 个答案:

答案 0 :(得分: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();

希望它有所帮助。

答案 1 :(得分: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);
    }
}
相关问题