This question already has answers here:
Closed last year.
How to make pipes work with Runtime.exec()?
(3个答案)
我试着执行这个:
    Process p = Runtime.getRuntime().exec("ps -ef | grep bash");

    BufferedReader r =  new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((line = r.readLine()) != null) {
      System.out.println(line);
    }

希望看到一些行,但它返回Null
如果我只执行"ps -ef"它将返回所有正确的进程。
一般我需要把关键字发送到方法,那个返回工作过程还是不返回

最佳答案

这样就行了

String[] command =  {"/bin/sh", "-c", "ps -ef | grep bash"};
Process p = Runtime.getRuntime().exec(command);

关于java - 命令行进程在Java中读取Linux ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51098328/

10-12 17:46