Java 将 Runtime exec() OutputStream 打印到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3936023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:11:47  来源:igfitidea点击:

Printing Runtime exec() OutputStream to console

javastreamruntimeruntime.execprintstream

提问by TheWolf

I am trying to get the OutputStreamof the Processinitiated by exec()to the console. How can this be done?

我正在尝试OutputStreamProcess发起的exec()发送到控制台。如何才能做到这一点?

Here is some incomplete code:

下面是一些不完整的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;

public class RuntimeTests
{
    public static void main(String[] args)
    {
        File path = new File("C:\Dir\Dir2");
        String command = "cmd /c dir";
        Reader rdr = null;
        PrintStream prtStrm = System.out;
        try
        {
            Runtime terminal = Runtime.getRuntime();

            OutputStream rtm = terminal.exec(command, null, path).getOutputStream();
            prtStrm = new PrintStream(rtm);
            prtStrm.println();
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

采纳答案by Eugene Kuleshov

You need to start a new thread that would read terminal output stream and copy it to the console, after you call process.waitFor()

在调用 process.waitFor() 之后,您需要启动一个新线程来读取终端输出流并将其复制到控制台

回答by Stijn Geukens

I believe this is what you're looking for:

我相信这就是你要找的:

  String line;
  Process p = Runtime.getRuntime().exec(...);
  BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
  while ((line = input.readLine()) != null) {
    System.out.println(line);
  }
  input.close();

回答by Benjamin Gruenbaum

I recently ran into this problem and just wanted to mention that since java 7 the process builder apihas been expanded. This problem can now be solved with:

我最近遇到了这个问题,只想提一下,自 java 7 以来,进程构建器 api已经扩展。这个问题现在可以解决:

ProcessBuilder pb = new ProcessBuilder("yourcommand");
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);
Process p = pb.start();

回答by samaitra

I faced the similar problem and I am using the following code.

我遇到了类似的问题,我正在使用以下代码。

Process p = Runtime.getRuntime().exec(".....");
p.waitFor();

String line;

BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while((line = error.readLine()) != null){
    System.out.println(line);
}
error.close();

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((line=input.readLine()) != null){
    System.out.println(line);
}

input.close();

OutputStream outputStream = p.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);
printStream.println();
printStream.flush();
printStream.close();

回答by yegor256

Try VerboseProcessfrom jcabi-log:

VerboseProcessjcabi-log尝试:

String output = new VerboseProcess(new ProcessBuilder("foo.bat")).stdout();

The class starts a background thread, listens to the output stream, and logs it.

该类启动一个后台线程,监听输出流并记录它。

回答by radiantRazor

If you can use org.apache.commons.io.IOUTils from commons-io,

如果您可以使用来自 commons-io 的 org.apache.commons.io.IOUTils,

System.out.println(IOUtils.toString(process.getInputStream()));
System.err.println(IOUtils.toString(process.getErrorStream()));

回答by Mohamed Anees A

I know this is a very old question, but a better alternate for the above answers would be

我知道这是一个非常古老的问题,但上述答案的更好替代方案是

ProcessBuilder builder = new ProcessBuilder(command);
builder.inheritIO();
Process p = builder.start();

From the docs of ProcessBuilder.inheritIO(),

从文档中ProcessBuilder.inheritIO()

Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.

将子进程标准 I/O 的源和目标设置为与当前 Java 进程的源和目标相同。

Hope this helps someone!

希望这可以帮助某人!