Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
jaideep.barde@gmail.com - 07 Aug 2006 10:41 GMT
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
Gordon Beaton - 07 Aug 2006 11:52 GMT
> 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,
[quoted text clipped - 7 lines]
> 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

Signature

[ 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

jaibarde@gmail.com - 08 Aug 2006 06:39 GMT
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

> > 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,
[quoted text clipped - 37 lines]
> [ 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
Gordon Beaton - 08 Aug 2006 07:40 GMT
> 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

Signature

[ 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

jaideep.barde@gmail.com - 08 Aug 2006 09:31 GMT
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

> > One more thing, Gordon, can you pls elaborate more on your quote ,
> >> Note too that your specific example is easily implemented in pure Java.
[quoted text clipped - 16 lines]
> [ 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.