Java中的子进程再次出现问题

我在Ubuntu 14.04上.

我试图运行像ps aux | grep whatevah通过
Java的类ProcessBuilder.我创建了两个子进程,我让它们同步通信,但由于某种原因,我在终端中看不到任何东西.

这是代码:

try {
    // What comes out of process1 is our inputStream
    Process process1   = new ProcessBuilder("ps", "aux").start();
    InputStream is1    = process1.getInputStream();
    BufferedReader br1 = new BufferedReader (new InputStreamReader(is1));

    // What goes into process2 is our outputStream
    Process process2  = new ProcessBuilder("grep", "gedit").start();
    OutputStream os   = process2.getOutputStream();
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));

    // Send the output of process1 to the input of process2
    String p1Output  = null;
    while ((p1Output = br1.readLine()) != null) {
        bw.write(p1Output);
        System.out.println(p1Output);
    }
    // Synchronization
    int finish = process2.waitFor();
    System.out.println(finish);

    // What comes out of process2 is our inputStream            
    InputStream is2    = process2.getInputStream();
    BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));

    String combOutput  = null;
    while ((combOutput = br2.readLine()) != null)
        System.out.println(combOutput);

    os.close();
    is1.close();
    is2.close();

} catch (IOException e) {
    System.out.println("Command execution error: " + e.getMessage());
} catch (Exception e) {
    System.out.println("General error: " + e.getMessage());
}

(System.out.println(p1Output);仅供我检查,必须工作的打印是最后一个,打印ps aux | grep whatevah的结果.)

我尝试了几件事,不太愚蠢的包括:

>如果我评论有关process2的所有内容,我会在终端上打印ps aux的结果
>如果我按原样运行程序,它不会向终端打印任何内容.
>如果我取消注释waitFor调用,则只打印ps aux.
>如果将命令更改为例如ls -al和ls -al,则两者都打印出来.
>我尝试将“aux”更改为“aux |”但仍然没有打印.
>关闭缓冲区,也没有

等等

任何帮助将非常感谢.
干杯!

编辑

在接受Ryan的惊人答案后几分钟,我做了最后一次尝试使这段代码工作.我成功了!我变了:

while ((p1Output = br1.readLine()) != null) {
    bw.write(p1Output);
    System.out.println(p1Output);
}

对于:

while ((p1Output = br1.readLine()) != null) {
    bw.write(p1Output + "\n");
    System.out.println(p1Output);
}

bw.close();

它的工作原理!我记得之前关闭缓冲区,所以我不知道出了什么问题.事实证明,你不应该保持清醒直到最近试图使一段代码工作XD.

不过,Ryan在这里的回答仍然令人惊讶.

最佳答案 鉴于评论中的建议,需要注意的重要一点是必须使用线程来处理进程的输入/输出,以实现您想要的.

我使用了jtahlborn发布的链接并调整了您可以使用的解决方案.

我创建了一个简单的示例,它将列出目录中的文件并通过输出进行grep.
此示例模拟命令ls -1 | grep some from一个名为test的目录,包含三个文件somefile.txt someotherfile.txt和this_other_file.csv

编辑:原始解决方案并没有真正完全使用“管道”方法,因为它在启动p2之前完全等待p1完成.相反,它应该启动它们,然后第一个的输出应该通过管道输送到第二个.我用一个完成此任务的类更新了解决方案.

import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try {
            // construct a process
            ProcessBuilder pb1 = new ProcessBuilder("ls", "-1");
            // set working directory
            pb1.directory(new File("test"));

            // start process
            final Process process1 = pb1.start();

            // get input/error streams
            final InputStream p1InStream = process1.getInputStream();
            final InputStream p1ErrStream = process1.getErrorStream();

            // handle error stream
            Thread t1Err = new InputReaderThread(p1ErrStream, "Process 1 Err");
            t1Err.start();

            // this will print out the data from process 1 (for illustration purposes)
            // and redirect it to process 2
            Process process2  = new ProcessBuilder("grep", "some").start();

            // process 2 streams
            final InputStream p2InStream = process2.getInputStream();
            final InputStream p2ErrStream = process2.getErrorStream();
            final OutputStream p2OutStream = process2.getOutputStream();

            // do the same as process 1 for process 2...
            Thread t2In = new InputReaderThread(p2InStream, "Process 2 Out");
            t2In.start();
            Thread t2Err = new InputReaderThread(p2ErrStream, "Process 2 Err");
            t2Err.start();

            // create a new thread with our pipe class
            // pass in the input stream of p1, the output stream of p2, and the name of the input stream
            new Thread(new PipeClass(p1InStream, p2OutStream, "Process 1 Out")).start();

            // wait for p2 to finish
            process2.waitFor();

        } catch (IOException e) {
            System.out.println("Command execution error: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("General error: " + e.getMessage());
        }
    }
}

这是一个用于模拟过程管道的类.它使用一些循环来复制字节,并且可以更高效,这取决于您的需要,但为了说明,它应该工作.

// this class simulates a pipe between two processes
public class PipeClass implements Runnable {
    // the input stream
    InputStream is;
    // the output stream
    OutputStream os;
    // the name associated with the input stream (for printing purposes only...)
    String isName;

    // constructor
    public PipeClass(InputStream is, OutputStream os, String isName) {
        this.is = is;
        this.os = os;
        this.isName = isName;
    }

    @Override
    public void run() {
        try {
            // use a byte array output stream so we can clone the data and use it multiple times
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            // read the data into the output stream (it has to fit in memory for this to work...)
            byte[] buffer = new byte[512]; // Adjust if you want
            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1) {
                baos.write(buffer, 0, bytesRead);
            }

            // clone it so we can print it out
            InputStream clonedIs1 = new ByteArrayInputStream(baos.toByteArray());
            Scanner sc = new Scanner(clonedIs1);

            // print the info
            while (sc.hasNextLine()) {
                System.out.println(this.isName + " >> " + sc.nextLine());
            }

            // clone again to redirect to the output of the other process
            InputStream clonedIs2 = new ByteArrayInputStream(baos.toByteArray());
            buffer = new byte[512]; // Adjust if you want
            while ((bytesRead = clonedIs2.read(buffer)) != -1) {
                // write it out to the output stream
                os.write(buffer, 0, bytesRead);
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        finally {
            try {
                // close so the process will finish
                is.close();
                os.close();
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

这是一个为处理流程输出而创建的类,改编自this reference

// Thread reader class adapted from
// http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
public class InputReaderThread extends Thread {
    // input stream
    InputStream is;
    // name
    String name;
    // is there data?
    boolean hasData = false;
    // data itself
    StringBuilder data = new StringBuilder();


    // constructor
    public InputReaderThread(InputStream is, String name) {
        this.is = is;
        this.name = name;
    }

    // set if there's data to read
    public synchronized void setHasData(boolean hasData) {
        this.hasData = hasData;
    }

    // data available?
    public boolean hasData() { return this.hasData; }

    // get the data
    public StringBuilder getData() {
        setHasData(false);  // clear flag
        StringBuilder returnData = this.data;
        this.data = new StringBuilder();

        return returnData;
    }

    @Override
    public void run() {
        // input reader
        InputStreamReader isr = new InputStreamReader(this.is);
        Scanner sc = new Scanner(isr);
        // while data remains
        while ( sc.hasNextLine() ) {
            // print out and append to data
            String line = sc.nextLine();
            System.out.println(this.name + " >> " + line);
            this.data.append(line + "\n");
        }
        // flag there's data available
        setHasData(true);
    }
}

产生的输出是:

Process 1 Out >> somefile.txt
Process 1 Out >> someotherfile.txt
Process 1 Out >> this_other_file.csv
Process 2 Out >> somefile.txt
Process 2 Out >> someotherfile.txt

要显示管道确实正常工作,请将命令更改为ps -a | grep usr输出是:

Process 1 Out >>       PID    PPID    PGID     WINPID  TTY  UID    STIME COMMAND
Process 1 Out >> I   15016       1   15016      15016  con  400 13:45:59 /usr/bin/grep
Process 1 Out >>     15156       1   15156      15156  con  400 14:21:54 /usr/bin/ps
Process 1 Out >> I    9784       1    9784       9784  con  400 14:21:54 /usr/bin/grep
Process 2 Out >> I   15016       1   15016      15016  con  400 13:45:59 /usr/bin/grep
Process 2 Out >>     15156       1   15156      15156  con  400 14:21:54 /usr/bin/ps
Process 2 Out >> I    9784       1    9784       9784  con  400 14:21:54 /usr/bin/grep

在过程2的输出中看到grep命令显示管道正在工作,我发布的旧解决方案将丢失.

请注意错误流的处理,即使您不打算使用它,这也是一种很好的做法.

这是一个快速而肮脏的解决方案,可以从一些额外的线程管理技术中受益,但它应该可以满足您的需求.

点赞