Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Facing problem while using "|" (pipe) in Runtime.exec

Reply
Thread Tools

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

 
 
jaideep.barde@gmail.com
Guest
Posts: n/a
 
      08-07-2006
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

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      08-07-2006
On 7 Aug 2006 02:41:48 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
jaibarde@gmail.com
Guest
Posts: n/a
 
      08-08-2006
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 Beaton wrote:
> On 7 Aug 2006 02:41:48 -0700, (E-Mail Removed) 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


 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      08-08-2006
On 7 Aug 2006 22:39:08 -0700, (E-Mail Removed) wrote:
> 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.


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

--
[ 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
 
Reply With Quote
 
jaideep.barde@gmail.com
Guest
Posts: n/a
 
      08-08-2006
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

Gordon Beaton wrote:
> On 7 Aug 2006 22:39:08 -0700, (E-Mail Removed) wrote:
> > 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.

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
facing problem while using regexec apoorva.groups@gmail.com C Programming 2 12-07-2007 05:07 PM
Facing problem while accessing virtual server count in Sharpeoint using SPGlobalAdmin sureshhalade@gmail.com ASP .Net Security 0 06-06-2007 09:41 AM
problem facing while using ostream_iterator alzforu@yahoo.com C++ 2 11-13-2006 11:48 AM
Facing problem while using Wizards with Project Templates !!!!! Anubhav Jain ASP .Net 0 03-20-2006 09:16 AM



Advertisments