如何在Java中通过exec使用管道符号

发布于 2024-12-02 09:06:30 字数 682 浏览 0 评论 0原文

我使用以下代码来获取系统中运行的所有进程的详细信息:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

想你只要分分秒秒 2024-12-09 09:06:30

好吧,如果你想在你执行的进程中使用管道,那么你需要启动一个像bash这样的shell,并在命令行上给那个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

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 -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.

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

满意归宿 2024-12-09 09:06:30

管道是一个 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 a Matcher and only pay attention to the lines that include java from your InputStream

http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html

玩心态 2024-12-09 09:06:30
  1. 调用 exec 时需要将命令与其参数分开,例如 .exec(new String[] { "ps", "aux" })不是 exec("ps aux"))。当您需要传递参数时,您需要调用 String[] 版本 - String[] 的第一个元素是命令,其余的都是参数。

  2. 您需要将第一个命令的输出流的内容发送到第二个命令的输入流。我使用 Apache commons IOUtils 来做

  3. 您必须关闭 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<String> result = IOUtils.readLines(p2.getInputStream());
    System.out.println(result);
}
  1. You need to separate the command from its arguments when calling exec, eg .exec(new String[] { "ps", "aux" }) (not exec("ps aux")). When you need to pass arguments, you need to invoke the String[] version - the first element of the String[] is the command, the rest are the arguments.

  2. 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.

  3. 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:

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);
}
人间不值得 2024-12-09 09:06:30

抱歉,我不知道如何正确地将结果从一个命令传输到另一个命令,但如果您想要的只是 Java 进程,那么 JDK 附带了一个名为 jps 的程序,它正是执行此操作的。试试这个:

jps -l

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:

jps -l
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文