On 7 Aug 2006 02:41:48 -0700,
wrote:
> 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
--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e