java 如何在Java中通过exec使用管道符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7226538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Pipe Symbol through exec in Java
提问by Vinesh
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:
我想ps aux
用管道符号过滤,所以我使用这个:
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:
它转到 ErrorStream。后来我注意到管道符号 (|) 在 Java 中用作按位包含 OR 运算符。所以我在管道符号前面使用了反斜杠,如下所示:
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?
但它再次转到 ErrorStream。如何ps aux | grep java
在 Java 中运行exec?
采纳答案by Brian Roach
The pipe is a shell feature - you're not using a shell, you're exec'ing a process (ps
).
管道是一个 shell 特性——你不是在使用 shell,而是在执行一个进程 ( 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"
“执行 ps,然后将其输出通过管道传输到另一个程序 (grep) 并让它提取我需要的内容”
You just need to extract what you want from the output of ps
. Use a Matcher
and only pay attention to the lines that include java
from your InputStream
您只需要从ps
. 使用 aMatcher
并且只注意java
从你的InputStream
http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html
http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html
回答by Angel O'Sphere
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:
好吧,如果你想在你执行的进程中使用管道,那么你需要启动一个像 bash 这样的 shell,并在命令行上给那个 shell 提供 ps 和 grep :
bash -c "echo $PATH"
Try this to understand what I mean, then use this:
试试这个来理解我的意思,然后使用这个:
bash -c "ps axu | grep PATTERN"
to understand how you need to set up your java.runtime.Process.
了解您需要如何设置 java.runtime.Process。
I did not try it but I assume this:
我没有尝试过,但我认为:
Process p = Runtime.getRuntime().exec(new String[] { "bash", "-c", "ps axu | grep PATTERN" });
Hope that helps ;D
希望有帮助;D
回答by Bohemian
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 IOUtilsto do this with ease.
You must close the input stream of the
grep
call, otherwise it will hang waiting for the end of input.
调用 exec 时,您需要将命令与其参数分开,例如
.exec(new String[] { "ps", "aux" })
( notexec("ps aux")
)。当需要传递参数时,需要调用 String[] 版本——第一个元素String[]
是命令,其余是参数。您需要将第一个命令的输出流的内容发送到第二个命令的输入流。我使用Apache commons IOUtils轻松完成此操作。
必须关闭
grep
调用的输入流,否则会挂起等待输入结束。
With these changes in place, this code does what you want:
完成这些更改后,此代码将执行您想要的操作:
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<String> result = IOUtils.readLines(p2.getInputStream());
System.out.println(result);
}
回答by Nate W.
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:
抱歉,我不知道如何正确地将结果从一个命令传送到另一个命令,但是如果您只想要 Java 进程,那么 JDK 附带了一个名为的程序jps
,它正是这样做的。试试这个:
jps -l