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

Discussion in 'Java' started by jaideep.barde@gmail.com, Aug 7, 2006.

  1. Guest

    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
    , Aug 7, 2006
    #1
    1. Advertising

  2. 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
    Gordon Beaton, Aug 7, 2006
    #2
    1. Advertising

  3. Guest

    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, 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
    , Aug 8, 2006
    #3
  4. On 7 Aug 2006 22:39:08 -0700, 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
    Gordon Beaton, Aug 8, 2006
    #4
  5. Guest

    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, 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
    , Aug 8, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anubhav Jain
    Replies:
    0
    Views:
    430
    Anubhav Jain
    Mar 20, 2006
  2. Jon Wayne
    Replies:
    0
    Views:
    530
    Jon Wayne
    Jun 29, 2004
  3. Hal Vaughan
    Replies:
    11
    Views:
    1,084
    Gordon Beaton
    May 22, 2006
  4. Replies:
    2
    Views:
    457
    Pete Becker
    Nov 13, 2006
  5. facing problem while using regexec

    , Dec 7, 2007, in forum: C Programming
    Replies:
    2
    Views:
    519
    Ben Bacarisse
    Dec 7, 2007
Loading...

Share This Page