java command line space & escape character parser for Runtime.exec

Discussion in 'Java' started by jimmy_please@yahoo.com, Oct 30, 2006.

  1. Guest

    Hi all,

    I'm trying to execute a string of command using Runtime.exec. This
    string may contain space or other meta-charcters which has been
    escaped. Does anyone know any tools out there does the parsing and
    possibly put them into an array or in a collection for me?

    i.e. on the extreme side, here is a possible string (assuming user
    always uses double slashes)
    "\"C:\\My Test\\my test batch.bat" argA \"arg B\" \"arg\ C\" \"arg\tD\"
    \"arg \/E\""

    This may or may not be an everyday usecase, but it does happens.

    Does anyone out there know how java.exe parse the command line string
    to a string array which then used by "static void main(String[] args)"?

    Thank you guys!
    Jimmy
    , Oct 30, 2006
    #1
    1. Advertising

  2. Hi Jimmy,

    > i.e. on the extreme side, here is a possible string (assuming user
    > always uses double slashes)
    > "\"C:\\My Test\\my test batch.bat" argA \"arg B\" \"arg\ C\" \"arg\tD\"
    > \"arg \/E\""


    When you call Runtime.exec(), you don't need to have all the arguments
    quoted.
    Instead, use one of the forms of exec() that accepts a string array -
    cmdarray - as the first argument. For example:

    exec(String[] cmdarray)

    The first string is the command and each string after that is a command
    line argument. Of course, this assumes you have control over how the
    input to your code gets formatted, so it might not help you...

    Rob
    :)
    Robert Mark Bram, Oct 31, 2006
    #2
    1. Advertising

  3. Paul Hamaker Guest

    Just a note: Better not use spaces in dir/filenames.
    Paul Hamaker, Oct 31, 2006
    #3
  4. Guest

    Thanks for opions guys. Unfortunately, I have no control on whether if
    the directory contains space or not. So double-quotes need to be used
    some how. I can write a little parser to do that but I thought if
    there's open-source tool does that and it will be great.
    Tools like CLI from Apache is expected a pre-parse string array to feed
    in ... is there a library out there will take the string as it is for
    parsing? ... and just through ParseException of some sort to identicate
    there's a format problem?
    , Oct 31, 2006
    #4
  5. Hi Jimmy,

    > Thanks for opions guys. Unfortunately, I have no control on whether if
    > the directory contains space or not. So double-quotes need to be used
    > some how. I can write a little parser to do that but I thought if
    > there's open-source tool does that and it will be great.
    > Tools like CLI from Apache is expected a pre-parse string array to feed
    > in ... is there a library out there will take the string as it is for
    > parsing? ... and just through ParseException of some sort to identicate
    > there's a format problem?


    I don't know about an external API, but the exec method I mentioned
    handles the quoting of arguments for you - try it out and you will find
    that if you have "arg two" in the 3rd position of the arg array, it
    will come out as being a whole second arg, not arg two and three.

    Rob
    :)
    Robert Mark Bram, Oct 31, 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. Charlie Zhang
    Replies:
    5
    Views:
    6,515
    Qu├ębec
    Jun 19, 2004
  2. Bernard
    Replies:
    4
    Views:
    5,355
    Raymond DeCampo
    Jan 3, 2006
  3. Hal Vaughan
    Replies:
    11
    Views:
    1,084
    Gordon Beaton
    May 22, 2006
  4. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,862
    Bryan Bullard
    Jul 11, 2003
  5. slomo
    Replies:
    5
    Views:
    1,501
    Duncan Booth
    Dec 2, 2007
Loading...

Share This Page