Starting a process with inherited stdin/stdout/stderr in Java 6

后端 未结 3 2000
鱼传尺愫
鱼传尺愫 2020-12-05 13:31

If I start a process via Java\'s ProcessBuilder class, I have full access to that process\'s standard in, standard out, and standard error streams as Java InputStreams

相关标签:
3条回答
  • 2020-12-05 13:50

    You will need to copy the Process out, err, and input streams to the System versions. The easiest way to do that is using the IOUtils class from the Commons IO package. The copy method looks to be what you need. The copy method invocations will need to be in separate threads.

    Here is the basic code:

    // Assume you already have a processBuilder all configured and ready to go
    final Process process = processBuilder.start();
    new Thread(new Runnable() {public void run() {
      IOUtils.copy(process.getOutputStream(), System.out);
    } } ).start();
    new Thread(new Runnable() {public void run() {
      IOUtils.copy(process.getErrorStream(), System.err);
    } } ).start();
    new Thread(new Runnable() {public void run() {
      IOUtils.copy(System.in, process.getInputStream());
    } } ).start();
    
    0 讨论(0)
  • 2020-12-05 13:52

    A variation on John's answer that compiles and doesn't require you to use Commons IO:

    private static void pipeOutput(Process process) {
        pipe(process.getErrorStream(), System.err);
        pipe(process.getInputStream(), System.out);
    }
    
    private static void pipe(final InputStream src, final PrintStream dest) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    byte[] buffer = new byte[1024];
                    for (int n = 0; n != -1; n = src.read(buffer)) {
                        dest.write(buffer, 0, n);
                    }
                } catch (IOException e) { // just exit
                }
            }
        }).start();
    }
    
    0 讨论(0)
  • 2020-12-05 13:58

    For System.in use the following pipein() instead of pipe()

    pipein(System.in, p.getOutputStream());
    

    Implementation:

    private static void pipein(final InputStream src, final OutputStream dest) {
    
        new Thread(new Runnable() {
            public void run() {
                try {
                   int ret = -1;
                   while ((ret = System.in.read()) != -1) {
                      dest.write(ret);
                      dest.flush();
                   }
                } catch (IOException e) { // just exit
                }
            }
        }).start();
    
    }
    
    0 讨论(0)
提交回复
热议问题