Runtime.exec help please

Discussion in 'Java' started by Steve Rainbird, Jul 25, 2007.

  1. I am trying to use Runtime.exec to run a command and pass it a parameter.

    I need it to preserve the spaces in the parameter but cannot get it to work.

    E.g

    My code says

    String cmd = "sh1 xxxx 1234";
    Process child = Runtime.getRuntime().exec(cmd);

    the sh1 bash shell says

    echo '$@' = $@
    echo '$1' = $1
    echo '$2' = $2

    When I run it it displays

    $@ = xxxx 1234
    $1 = xxxx
    $2 = 1234

    I want it to pass the code "as is" so as one parameter

    So it would display

    $@ = xxxx 1234
    $1 = xxxx 1234
    $2 =

    I could even cope with

    $@ = xxxx 1234
    $1 = xxxx
    $2 = 1234


    I have tried different variation of single and double quotes without any
    success.

    --
    Steve
     
    Steve Rainbird, Jul 25, 2007
    #1
    1. Advertising

  2. Steve Rainbird

    Daniel Pitts Guest

    On Jul 25, 6:56 am, "Steve Rainbird"
    <> wrote:
    > I am trying to use Runtime.exec to run a command and pass it a parameter.
    >
    > I need it to preserve the spaces in the parameter but cannot get it to work.
    >
    > E.g
    >
    > My code says
    >
    > String cmd = "sh1 xxxx 1234";
    > Process child = Runtime.getRuntime().exec(cmd);
    >
    > the sh1 bash shell says
    >
    > echo '$@' = $@
    > echo '$1' = $1
    > echo '$2' = $2
    >
    > When I run it it displays
    >
    > $@ = xxxx 1234
    > $1 = xxxx
    > $2 = 1234
    >
    > I want it to pass the code "as is" so as one parameter
    >
    > So it would display
    >
    > $@ = xxxx 1234
    > $1 = xxxx 1234
    > $2 =
    >
    > I could even cope with
    >
    > $@ = xxxx 1234
    > $1 = xxxx
    > $2 = 1234
    >
    > I have tried different variation of single and double quotes without any
    > success.
    >
    > --
    > Steve


    did you try:
    String command = "sh1 \"first second\"";
    Runtime.getRuntime().exec(command);

    Alternatively, look into the the ProcessBuilder class, it gives you
    more control over individual parameters. Its also more secure if
    you're getting data from the user. Not completely secure mind you,
    but it does make sure that arguments are escaped appropriately for
    your shell.
     
    Daniel Pitts, Jul 25, 2007
    #2
    1. Advertising

  3. "Daniel Pitts" <> wrote in message
    news:...
    > On Jul 25, 6:56 am, "Steve Rainbird"
    > <> wrote:
    >> I am trying to use Runtime.exec to run a command and pass it a parameter.
    >>
    >> I need it to preserve the spaces in the parameter but cannot get it to
    >> work.
    >>
    >> E.g
    >>
    >> My code says
    >>
    >> String cmd = "sh1 xxxx 1234";
    >> Process child = Runtime.getRuntime().exec(cmd);
    >>
    >> the sh1 bash shell says
    >>
    >> echo '$@' = $@
    >> echo '$1' = $1
    >> echo '$2' = $2
    >>
    >> When I run it it displays
    >>
    >> $@ = xxxx 1234
    >> $1 = xxxx
    >> $2 = 1234
    >>
    >> I want it to pass the code "as is" so as one parameter
    >>
    >> So it would display
    >>
    >> $@ = xxxx 1234
    >> $1 = xxxx 1234
    >> $2 =
    >>
    >> I could even cope with
    >>
    >> $@ = xxxx 1234
    >> $1 = xxxx
    >> $2 = 1234
    >>
    >> I have tried different variation of single and double quotes without any
    >> success.
    >>
    >> --
    >> Steve

    >
    > did you try:
    > String command = "sh1 \"first second\"";
    > Runtime.getRuntime().exec(command);


    Yep tried that.

    I got

    $@ = "xxxx 1234"
    $1 = "xxxx
    $2 = 1234"


    BTW I have unset IFS in the shell

    so if I just execute

    sh1 "xxxxxx 1234"
    it does exaclty what I need

    $@ = xxxxxx 1234
    $1 = xxxxxx 1234
    $2 =

    >
    > Alternatively, look into the the ProcessBuilder class, it gives you
    > more control over individual parameters. Its also more secure if
    > you're getting data from the user. Not completely secure mind you,
    > but it does make sure that arguments are escaped appropriately for
    > your shell.
    >


    I will look into ProcessBuilder thanks.

    --
    Steve
     
    Steve Rainbird, Jul 25, 2007
    #3
  4. "Steve Rainbird" <> wrote in message
    news:...
    > "Daniel Pitts" <> wrote in message
    > news:...
    >> On Jul 25, 6:56 am, "Steve Rainbird"
    >> <> wrote:
    >>> I am trying to use Runtime.exec to run a command and pass it a
    >>> parameter.
    >>>
    >>> I need it to preserve the spaces in the parameter but cannot get it to
    >>> work.
    >>>
    >>> E.g
    >>>
    >>> My code says
    >>>
    >>> String cmd = "sh1 xxxx 1234";
    >>> Process child = Runtime.getRuntime().exec(cmd);
    >>>
    >>> the sh1 bash shell says
    >>>
    >>> echo '$@' = $@
    >>> echo '$1' = $1
    >>> echo '$2' = $2
    >>>
    >>> When I run it it displays
    >>>
    >>> $@ = xxxx 1234
    >>> $1 = xxxx
    >>> $2 = 1234
    >>>
    >>> I want it to pass the code "as is" so as one parameter
    >>>
    >>> So it would display
    >>>
    >>> $@ = xxxx 1234
    >>> $1 = xxxx 1234
    >>> $2 =
    >>>
    >>> I could even cope with
    >>>
    >>> $@ = xxxx 1234
    >>> $1 = xxxx
    >>> $2 = 1234
    >>>
    >>> I have tried different variation of single and double quotes without any
    >>> success.
    >>>
    >>> --
    >>> Steve

    >>
    >> did you try:
    >> String command = "sh1 \"first second\"";
    >> Runtime.getRuntime().exec(command);

    >
    > Yep tried that.
    >
    > I got
    >
    > $@ = "xxxx 1234"
    > $1 = "xxxx
    > $2 = 1234"
    >
    >
    > BTW I have unset IFS in the shell
    >
    > so if I just execute
    >
    > sh1 "xxxxxx 1234"
    > it does exaclty what I need
    >
    > $@ = xxxxxx 1234
    > $1 = xxxxxx 1234
    > $2 =
    >
    >>
    >> Alternatively, look into the the ProcessBuilder class, it gives you
    >> more control over individual parameters. Its also more secure if
    >> you're getting data from the user. Not completely secure mind you,
    >> but it does make sure that arguments are escaped appropriately for
    >> your shell.
    >>

    >
    > I will look into ProcessBuilder thanks.
    >
    > --
    > Steve
    >
    >
    >




    ProcessBuilder seems to be the solution thanks.

    String cmd = "sh1";
    String arg = "xxxx 1234";
    Process child = new ProcessBuilder(cmd,arg).start();

    $@ = "xxxx 1234"
    $1 = "xxxx 1234"
    $2 =

    --
    Steve
     
    Steve Rainbird, Jul 25, 2007
    #4
  5. Steve Rainbird

    bugbear Guest

    Steve Rainbird wrote:
    > I am trying to use Runtime.exec to run a command and pass it a parameter.
    >
    > I need it to preserve the spaces in the parameter but cannot get it to work.


    Consider "the other" entry point:
    public Process exec(String[] cmdarray)
    throws IOException

    The parameters are "nicely separate" in the array,
    as opposed to the entry point you're using:

    public Process exec(String command)
    throws IOException

    I think what's causing your trouble is this behaviour:

    >>The command argument is parsed into tokens
    >>and then executed as a command in a separate process.


    Reading documentation is sometimes helpful.

    BugBear
     
    bugbear, Jul 25, 2007
    #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. KK
    Replies:
    2
    Views:
    664
    Big Brian
    Oct 14, 2003
  2. Hal Vaughan
    Replies:
    11
    Views:
    1,178
    Gordon Beaton
    May 22, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,149
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    486
    Duncan Booth
    Jul 22, 2004
  5. Guillermo Riojas
    Replies:
    0
    Views:
    191
    Guillermo Riojas
    Nov 26, 2010
Loading...

Share This Page