如何在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中用作按位或运算符。因此,我在管道符号前面使用了反斜杠,如下所示:
Process p = Runtime.getRuntime().exec("ps aux \\| grep java");
但它再次转到 ErrorStream。我如何运行 ps aux | grep java 通过Java中的exec?
I am using the following code to get the details of all process running in the system:
Process p = Runtime.getRuntime().exec("ps aux");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
I want to filter ps aux
down with the pipe symbol so I use this:
Process p = Runtime.getRuntime().exec("ps aux | grep java");
It goes to the ErrorStream. Later I noticed the Pipe Symbol (|) is used as Bitwise inclusive OR operator in Java. So I used backslash in front of pipe symbol as shown below:
Process p = Runtime.getRuntime().exec("ps aux \\| grep java");
But again it goes to the ErrorStream. How can I run ps aux | grep java
through exec in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,如果你想在你执行的进程中使用管道,那么你需要启动一个像bash这样的shell,并在命令行上给那个shell提供ps和grep:
尝试这个来理解我的意思,然后使用这个
:了解如何设置 java.runtime.Process。
我没有尝试过,但我认为:
希望有帮助;D
Well, if you want to use pipes in the Process you exec, then you need to start a shell like bash, and give ps and grep on the command line to that shell:
Try this to understand what I mean, then use this:
to understand how you need to set up your java.runtime.Process.
I did not try it but I assume this:
Hope that helps ;D
管道是一个 shell 功能 - 您没有使用 shell,您正在执行一个进程 (
ps
)。但说实话,你为什么要这样做呢?你的意思是:
“执行 ps,然后将其输出传输到另一个程序(grep)并让它提取我需要的内容”
你只需要从
ps
的输出中提取你想要的内容。使用Matcher
并仅注意InputStream
中包含java
的行http://download.oracle.com/javase/6/docs/api/java/util/regex/匹配器.html
The pipe is a shell feature - you're not using a shell, you're exec'ing a process (
ps
).But really, why would you want to do this? What you're saying is:
"execute ps, then pipe its output to another program (grep) and have it extract what I need"
You just need to extract what you want from the output of
ps
. Use aMatcher
and only pay attention to the lines that includejava
from yourInputStream
http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html
调用 exec 时需要将命令与其参数分开,例如
.exec(new String[] { "ps", "aux" })
(不是exec("ps aux")
)。当您需要传递参数时,您需要调用 String[] 版本 -String[]
的第一个元素是命令,其余的都是参数。您需要将第一个命令的输出流的内容发送到第二个命令的输入流。我使用 Apache commons IOUtils 来做
您必须关闭
grep
调用的输入流,否则它将挂起等待输入结束。完成这些更改后,此代码将执行您想要的操作:
You need to separate the command from its arguments when calling exec, eg
.exec(new String[] { "ps", "aux" })
(notexec("ps aux")
). When you need to pass arguments, you need to invoke the String[] version - the first element of theString[]
is the command, the rest are the arguments.You need to send the contents of the output stream of the first command to the input stream of the second command. I used Apache commons IOUtils to do this with ease.
You must close the input stream of the
grep
call, otherwise it will hang waiting for the end of input.With these changes in place, this code does what you want:
抱歉,我不知道如何正确地将结果从一个命令传输到另一个命令,但如果您想要的只是 Java 进程,那么 JDK 附带了一个名为
jps
的程序,它正是执行此操作的。试试这个:Sorry I do not know how to correctly pipe results from one command to another, but if all you want is the Java process, the JDK comes with a program called
jps
that does exactly this. Try this: