简体   繁体   English

不同的ProcessBuilder.redirectInput()有什么区别?

[英]What is the difference between different ProcessBuilder.redirectInput()?

I went through the documentation of ProcessBuilder but I am having trouble understanding the difference between我浏览了 ProcessBuilder 的文档,但我无法理解两者之间的区别

  1. processBuiler.inheritIO() processBuiler.inheritIO()
  2. processBuilder.redirectInput(ProcessBuilder.Redirect.PIPE) processBuilder.redirectInput(ProcessBuilder.Redirect.PIPE)
  3. processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT) processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT)

FROM DOC: ProcessBuilder.Redirect PIPE: Indicates that subprocess I/O will be connected to the current Java process over a pipe. This is the default handling of subprocess standard I/O. FROM DOC:ProcessBuilder.Redirect PIPE:表示子进程 I/O 将通过 pipe 连接到当前 Java 进程。这是子进程标准 I/O 的默认处理。

  1. what is a pipe here?这里的 pipe 是什么?

  2. when and where would you use each one of them?您将在何时何地使用它们中的每一个?

  3. based on the docs what is the diff between 1 and 3 then?根据文档,1 和 3 之间的区别是什么?

If this question is not fit for stackoverflow, please point me in the right direction as to where to ask this.如果这个问题不适合 stackoverflow,请指出正确的方向,告诉我在哪里问这个问题。

PIPE means that the I/O streams of the process will be obtainable via Process#getInputStream , Process#getErrorStream and Process#getOutputStream . PIPE表示进程的 I/O 流可以通过Process#getInputStreamProcess#getErrorStreamProcess#getOutputStream获得。 This is different from INHERIT , which merges a stream with the stream of the current Java process, that is, System.out , System.in , or System.err .这与INHERIT不同,它将 stream 与当前 Java 进程的 stream 合并,即System.outSystem.inSystem.err

When you call ProcessBuilder#inheritIO , you are redirecting all I/O streams to the System .当您调用ProcessBuilder#inheritIO时,您将所有 I/O 流重定向到System

When you call redirectInput(ProcessBuilder.Redirect.PIPE) , you are redirecting only the input to the PIPE .当您调用redirectInput(ProcessBuilder.Redirect.PIPE)时,您只是将输入重定向到PIPE

When you call redirectInput(ProcessBuilder.Redirect.INHERIT) , you are redirecting only the input to the System .当您调用redirectInput(ProcessBuilder.Redirect.INHERIT)时,您只是将输入重定向到System

So, the difference between 1st and 3d is that 3d call redirects only the input of the process, while the 1st one redirects all I/O streams.因此,第一个和 3d 之间的区别在于 3d 调用仅重定向进程的输入,而第一个重定向所有 I/O 流。

You should use INHERIT when you want to use a stream of the Process via the stream of the System .当您想通过System的 stream 使用 Process 的 stream 时,您应该使用INHERIT

And you should use PIPE when you want to handle a stream of the Process separately.当您要单独处理 Process 的 stream 时,您应该使用PIPE Consider following example:考虑以下示例:

public class ProcessTest {
    @Test
    public void testProcess() throws IOException {

        //all streams go to System streams
        new ProcessBuilder("java","--version").inheritIO().start();

        //all streams are kept separate, and are obtained via .get...Stream() calls 
        Process process2=new ProcessBuilder("java","--version").start();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process2.getInputStream()));
        String s;
        while ((s=bufferedReader.readLine())!=null)
        {
            System.out.println(s);
        }
        bufferedReader.close();

        //only the output of the Process is going to the System.out
        new ProcessBuilder("java","--version").redirectOutput(ProcessBuilder.Redirect.INHERIT).start();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM