如何在Java中使用管道符号通过exec
我使用以下代码来获取系统中运行的所有进程的详细信息:
Process p = Runtime.getRuntime().exec("ps aux"); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
我想用管道符号过滤ps aux
,所以我用它:
Process p = Runtime.getRuntime().exec("ps aux | grep java");
它转到ErrorStream。 后来我注意到管道符号(|)在Java中用作Bitwise包含OR运算符。 所以我在管道符号前面使用了反斜杠,如下所示:
Process p = Runtime.getRuntime().exec("ps aux \\| grep java");
但它再次进入ErrorStream。 我怎么能运行ps aux | grep java
ps aux | grep java
通过exec在Java中?
管道是一个shellfunction – 你没有使用shell,你正在执行一个进程( ps
)。
但是,真的,你为什么要这样做? 你的意思是:
“执行ps,然后将其输出传递给另一个程序(grep)并让它提取我需要的东西”
你只需要从ps
的输出中提取你想要的东西。 使用Matcher
,只关注InputStream
中包含java
的行
http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html
好吧,如果你想在你的exec中使用管道,那么你需要像bash一样启动一个shell,并在命令行上给ps和grep:
bash -c "echo $PATH"
试试这个来理解我的意思,然后使用它:
bash -c "ps axu | grep PATTERN"
了解如何设置java.runtime.Process。
我没试过但是我假设这个:
Process p = Runtime.getRuntime().exec(new String[] { "bash", "-c", "ps axu | grep PATTERN" });
希望有所帮助; D.
-
调用exec时需要将命令与其参数分开,例如
.exec(new String[] { "ps", "aux" })
( 不是exec("ps aux")
)。 当你需要传递参数时,你需要调用String []版本 – String []的第一个元素是命令,其余的是参数。 -
您需要将第一个命令的输出流的内容发送到第二个命令的输入流。 我使用Apache commons IOUtils轻松完成这项工作。
-
您必须关闭
grep
调用的输入流,否则它将等待输入结束。
有了这些更改,此代码可以满足您的需求:
import org.apache.commons.io.IOUtils; public static void main(String[] args) throws Exception { Process p1 = Runtime.getRuntime().exec(new String[] { "ps", "aux" }); InputStream input = p1.getInputStream(); Process p2 = Runtime.getRuntime().exec(new String[] { "grep", "java" }); OutputStream output = p2.getOutputStream(); IOUtils.copy(input, output); output.close(); // signals grep to finish List result = IOUtils.readLines(p2.getInputStream()); System.out.println(result); }
抱歉,我不知道如何正确地将结果从一个命令传递给另一个命令,但如果你想要的只是Java进程,那么JDK会附带一个名为jps
的程序。 尝试这个:
jps -l