splitting string to arguments for Runtime.exec

I

ittay.dror

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
 
L

Lisa

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
 
A

Alan Moore

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top