splitting string to arguments for Runtime.exec

Discussion in 'Java' started by ittay.dror@gmail.com, Mar 30, 2005.

  1. Guest

    Hi,

    Can someone please post a code example of how to convert a string with
    execution command to array of arguments for Runtime.exec? (since
    Runtime.exec(String) parses the string with StringTokenizer, it won't
    handle quoted arguments very well).

    I've tried StreamTokenizer, but it doesn't handle quotes well: "c:\foo"
    converts to "c:foo"

    Thanx,
    Ittay
     
    , Mar 30, 2005
    #1
    1. Advertising

  2. Lisa Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > Can someone please post a code example of how to convert a string with
    > execution command to array of arguments for Runtime.exec? (since
    > Runtime.exec(String) parses the string with StringTokenizer, it won't
    > handle quoted arguments very well).
    >
    > I've tried StreamTokenizer, but it doesn't handle quotes well: "c:\foo"
    > converts to "c:foo"
    >
    > Thanx,
    > Ittay
    >


    try split
     
    Lisa, Mar 30, 2005
    #2
    1. Advertising

  3. Alan Moore Guest

    On 29 Mar 2005 23:56:59 -0800, wrote:

    >Can someone please post a code example of how to convert a string with
    >execution command to array of arguments for Runtime.exec? (since
    >Runtime.exec(String) parses the string with StringTokenizer, it won't
    >handle quoted arguments very well).
    >
    >I've tried StreamTokenizer, but it doesn't handle quotes well: "c:\foo"
    >converts to "c:foo"


    Assuming you're running at least JDK 1.4, a regex approach is probably
    the easiest:

    List parts = new ArrayList();
    Pattern p = Pattern.compile("\".*?\"|\\S+");
    Matcher m = p.matcher(cmdString);
    while (m.find())
    {
    parts.add(m.group());
    }
    String[] cmdArray = (String[])parts.toArray();
     
    Alan Moore, Mar 31, 2005
    #3
    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. Prime
    Replies:
    6
    Views:
    1,126
    Nigel Wade
    Dec 19, 2005
  2. John Ericson
    Replies:
    0
    Views:
    450
    John Ericson
    Jul 19, 2003
  3. Mark
    Replies:
    0
    Views:
    462
  4. John Dibling
    Replies:
    0
    Views:
    435
    John Dibling
    Jul 19, 2003
  5. Hal Vaughan
    Replies:
    11
    Views:
    1,199
    Gordon Beaton
    May 22, 2006
Loading...

Share This Page