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

后端 未结 3 1999
鱼传尺愫
鱼传尺愫 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: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();
    
    }
    

提交回复
热议问题