如何让 Redirect.INHERIT 和 System.setOut 一起工作 [英] How to make Redirect.INHERIT and System.setOut work together

查看:36
本文介绍了如何让 Redirect.INHERIT 和 System.setOut 一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个微不足道的问题,但我无法轻易找到答案.我有一个简单的 Java 程序:

System.setOut(new PrintStream(new File("stdout.txt")));......ProcessBuilder pb = new ProcessBuilder("...一些参数...");pb.inheritIO();pb.start().waitFor();

我的目的是将进程的所有输出(包括子 pb)存储在 stdout.txt 中.但是,这似乎不起作用,pb 的输出被重定向到我的进程父进程的标准输出,就好像 System.setOut(...) 从未被调用过一样.>

有什么办法可以使用 pb.inheritIO/pb.inheritOutput 方法重定向到 redirected System.out 吗?我错过了一些明显的东西吗?我知道我可以通过老方法在线程中手动读取孩子的标准输出,但这只是更麻烦.

干杯,杰克

解决方案

请看我的回答 https://stackoverflow.com/a/32342557/5226711:

<块引用>

ProcessBuilder 的 I/O 重定向不是重定向流而是文件描述符.即使将 System.out 设置为另一个流也不会更改初始标准输出的文件描述符.ProcessBuilder.inheritIO() 继承父进程的标准输出/输入/错误文件描述符.

PrintStream 实例没有传递给启动的操作系统进程,而是类似的东西(它是伪代码,我不认为它确实有效):

((FileOutputStream) System.out.out).getFD()

或者,更正确:

FileDescriptor.out

which is static final 表示它不会改变,即使您将 System.out 设置为不同的内容.

关于您的问题,这意味着您必须使用 FileProcessBuilder.redirectOutput(File) 而不是 PrintStream:

ProcessBuilder pb = new ProcessBuilder("...一些参数...");pb.redirectOutput(new File("stdout.txt"));pb.start().waitFor();

这只会将子进程的输出重定向到 stdout.txt.

如果你想从父进程和子进程中直接输出所有的输出,你必须将父进程的 System.out 重定向到文件并在父进程中捕获子进程的输出并将其输出到其重定向的 System.out:

System.setOut(new PrintStream(new File("stdout.txt")));ProcessBuilder pb = new ProcessBuilder("...一些参数...");进程 p = pb.start();BufferedReader childOutput = new BufferedReader(new InputStreamReader(p.getInputStream()));字符串线;while ((line = output.readLine()) != null) {System.out.println(line);//孩子的输出到 stdout.txt}

This may be a trivial question but I can't easily find an answer. I've got a simple program in Java:

System.setOut(new PrintStream(new File("stdout.txt")));
...
...
ProcessBuilder pb = new ProcessBuilder("... some args ...");
pb.inheritIO();
pb.start().waitFor();

My intention is to store all output of the process (including child pb) in the stdout.txt. But, this seem not to work and the output of pb is redirected to my process parent's standard output as if System.setOut(...) was never called.

Is there any way I can use pb.inheritIO/pb.inheritOutput methods to redirect to a redirected System.out? Am I missing something obvious? I know I can do it the old way by reading from the child's standard output manually in a thread but that's just more hassle.

Cheers, Jacek

解决方案

Please see my answer https://stackoverflow.com/a/32342557/5226711:

The I/O redirection of ProcessBuilder does not redirect streams but file descriptors. Even setting System.out to another stream does not change the file descriptor of the initial standard output. ProcessBuilder.inheritIO() inherits the parent process' standard output/input/error file descriptors.

The PrintStream instance is not passed to the started operating system process, but rather something like (it is pseudo code, I do not think it actually works):

((FileOutputStream) System.out.out).getFD()

Or, more correct:

FileDescriptor.out

which is static final indicating that it will not change, even if you set System.out to something different.

Regarding your question, this means you have to use a File and ProcessBuilder.redirectOutput(File) instead of a PrintStream:

ProcessBuilder pb = new ProcessBuilder("... some args ...");
pb.redirectOutput(new File("stdout.txt"));
pb.start().waitFor();

This redirects only the output of the child process to stdout.txt.

If you want to direct all output from both the parent and the child process, you have to redirect the parent's System.out to the file and capture the child's output in the parent and output it to its redirected System.out:

System.setOut(new PrintStream(new File("stdout.txt")));
ProcessBuilder pb = new ProcessBuilder("... some args ...");
Process p = pb.start();
BufferedReader childOutput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = output.readLine()) != null) {
    System.out.println(line); // child`s output goes to stdout.txt
}

这篇关于如何让 Redirect.INHERIT 和 System.setOut 一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆