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();