Facing problem while using "|" (pipe) in Runtime.exec


J

jaideep.barde

Hi Gurus,
I want to execute a command consisting of couple of "|" pipes in it.
For e.g. "dir | grep gpc | grep -v 25". So did this like,

String cmd = "dir | grep gpc | grep -v 25";
Process p = Runtime.getRuntime().exec(cmd);
.....
.....

However, I get the output of this execution is only till first pipe!
that is, I see the output of "dir | grep gpc" only the rest "grep -v
25" command does not get into act.

Does the pipe when used in Runtime.exec work something different? Am I
doing somthing wrong here? I'm doing this on windows (NT) but will also
be using the code in UNIX so would there be any problems?

Thanks in advance for any types of pointers.
~jaideep
 
Ad

Advertisements

G

Gordon Beaton

I want to execute a command consisting of couple of "|" pipes in it.
For e.g. "dir | grep gpc | grep -v 25". So did this like,

String cmd = "dir | grep gpc | grep -v 25";
Process p = Runtime.getRuntime().exec(cmd);
....
....

However, I get the output of this execution is only till first pipe!
that is, I see the output of "dir | grep gpc" only the rest "grep -v
25" command does not get into act.

Are you sure that even "grep gpc" is executed?

Redirection operators like pipes are a shell feature, but the command
passed to exec() isn't run in a command shell.

If you want shell features, run a shell:

String[] cmd = {
"/bin/sh",
"-c",
"dir | grep gpc | grep -v 25"
};

Process p = Runtime.getRuntime().exec(cmd);

For NT I believe you can do something similar using cmd /c.

Note the specific way the shell command and shell arguments are
separated from the command line itself, and that a String[] is
necessary for this.

Note too that your specific example is easily implemented in pure
Java.

/gordon
 
J

jaibarde

Hey Thanks Gordon,
That helped :). Yep, in unix I had to pass the String[] as you have
mentioned below, but in NT it worked in a single String, like, String
cmd = "cmd /c dir....."
So bottomline is for all shell specific feature one has to pass chell
as a first parameter to the execution.

One more thing, Gordon, can you pls elaborate more on your quote ,
Note too that your specific example is easily implemented in pure Java.

I did not quite understand it. Is there any better way of executing and
extenal command and get it's out put in the program.

Thanks,
~jaideep


Gordon said:
I want to execute a command consisting of couple of "|" pipes in it.
For e.g. "dir | grep gpc | grep -v 25". So did this like,

String cmd = "dir | grep gpc | grep -v 25";
Process p = Runtime.getRuntime().exec(cmd);
....
....

However, I get the output of this execution is only till first pipe!
that is, I see the output of "dir | grep gpc" only the rest "grep -v
25" command does not get into act.

Are you sure that even "grep gpc" is executed?

Redirection operators like pipes are a shell feature, but the command
passed to exec() isn't run in a command shell.

If you want shell features, run a shell:

String[] cmd = {
"/bin/sh",
"-c",
"dir | grep gpc | grep -v 25"
};

Process p = Runtime.getRuntime().exec(cmd);

For NT I believe you can do something similar using cmd /c.

Note the specific way the shell command and shell arguments are
separated from the command line itself, and that a String[] is
necessary for this.

Note too that your specific example is easily implemented in pure
Java.

/gordon
 
G

Gordon Beaton

One more thing, Gordon, can you pls elaborate more on your quote ,

I did not quite understand it. Is there any better way of executing and
extenal command and get it's out put in the program.

I was referring to the specific command you were having problems with,
which does not require running an external program at all:

dir | grep gpc | grep -v 25

You can do this in pure Java: read the directory with
File.listFiles(), then iterate over the resulting list to compare each
filename with "gpc", etc.

/gordon
 
Ad

Advertisements

J

jaideep.barde

Oh, I get it. But that was just an example, I wanted to run several
different command, gather their output and provide some consolidated
report against that output.

Thanks for all your time and help. May be will ping you later for some
more help :), I've just started picking up java
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top