如何捕获Java中的所有Bash输出

时间:2021-12-12 15:45:21

I already have Java code that can handle all the output from stdout and stderr, and that all works fine. It uses getInputStream() and getErrorStream();

我已经有了可以处理stdout和stderr的所有输出的Java代码,并且一切正常。它使用getInputStream()和getErrorStream();

If I enter a command like this in a Bash terminal:

如果我在Bash终端输入这样的命令:

export ANDROID_HOME=/usr/local/bin/android-sdk-linux;export PATH=${PATH}:/usr/local/bin/gradle-1.8/bin;cd /var/local/android/dev/GradleHelloWorld;gradle clean

It gets extra output like:

它得到额外的输出,如:

> Loading

>Loading Resolving dependencies ...

>Building

Until it gets the regular "end" output:

直到它获得常规的“结束”输出:

:clean UP-TO-DATE

BUILD SUCCESSFUL

Total time: 2 mins 37.424 secs

But after trying many different combinations of, the below, with and without the "-c":

但尝试了下面的许多不同组合,有和没有“-c”:

processBuilder = new ProcessBuilder("bash", "-c", sCmd);

proc = rt.exec(new String[] {"/bin/bash", "-c", commandS});

I know those are pretty much the same, but you get the idea. It only shows the "end" result, and not any of the other output before it.

我知道那几乎是一样的,但你明白了。它只显示“结束”结果,而不显示其前的任何其他输出。

For Windows I can get all the output with no problem, using:

对于Windows,我可以毫无问题地获得所有输出,使用:

proc = rt.exec("cmd.exe");

and the command string.

和命令字符串。

I don't know that much about Bash, so is there any other options I can try, or a different Java statement I can use to try to capture all the output using Java?

我不太了解Bash,所以我可以尝试其他任何选项,还是可以使用不同的Java语句尝试使用Java捕获所有输出?

Update:

I'm already doing this (most of the code):

我已经这样做了(大部分代码):

while ( true )
{

    if ( error_is.available() > 0 ) // Check Error Stream..
    {
        iBytesRead = error_is.read(tempBuf, 0, iTmpLength);
        if ( iBytesRead == -1 )
        {
             break;
        }

        baos.write(tempBuf, 0, iBytesRead);
   }
   else if ( out_is.available() > 0 )   // Check Output Stream..
   {
        iBytesRead = out_is.read(tempBuf, 0, iTmpLength);
        if ( iBytesRead == -1 )
        {
            break;
        }

        baos.write(tempBuf, 0, iBytesRead);
   }
}

It works fine in Windows using cmd.exe so I know it works, and it gets everything, but for Linux users, I have to use Bash, and I'm not getting some of the output, so that's my question..

它在Windows中使用cmd.exe工作正常,所以我知道它有效,并且它可以获得一切,但对于Linux用户,我必须使用Bash,而我没有得到一些输出,所以这是我的问题..

1 个解决方案

#1


0  

You can use InputStreamReader in conjunction with BufferedReader to get the output of the command:

您可以将InputStreamReader与BufferedReader结合使用以获取命令的输出:

    ProcessBuilder pb = new ProcessBuilder("your-command");
    Process p = pb.start();
    InputStreamReader isr = new InputStreamReader(p.getInputStream());
    BufferedReader br = new BufferedReader(isr);

    String lineRead;
    while ((lineRead = br.readLine()) != null) {
        LOGGER.info(lineRead);
    }
    int rc = p.waitFor();
    return rc; // successful return code should be zero!

#1


0  

You can use InputStreamReader in conjunction with BufferedReader to get the output of the command:

您可以将InputStreamReader与BufferedReader结合使用以获取命令的输出:

    ProcessBuilder pb = new ProcessBuilder("your-command");
    Process p = pb.start();
    InputStreamReader isr = new InputStreamReader(p.getInputStream());
    BufferedReader br = new BufferedReader(isr);

    String lineRead;
    while ((lineRead = br.readLine()) != null) {
        LOGGER.info(lineRead);
    }
    int rc = p.waitFor();
    return rc; // successful return code should be zero!