How to use Pipe Symbol through exec in Java [duplicate]

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

Leave a Comment