Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > splitting string to arguments for Runtime.exec

Reply
Thread Tools

splitting string to arguments for Runtime.exec

 
 
ittay.dror@gmail.com
Guest
Posts: n/a
 
      03-30-2005
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

 
Reply With Quote
 
 
 
 
Lisa
Guest
Posts: n/a
 
      03-30-2005

<> wrote in message
news: ps.com...
> 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


 
Reply With Quote
 
 
 
 
Alan Moore
Guest
Posts: n/a
 
      03-31-2005
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();


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting Strings in a map<string, string> byte8bits@gmail.com C++ 3 04-15-2008 06:24 PM
Simple error : method format(String, Object[]) is not applicable for the arguments (String, String) ankur Java 1 08-27-2007 06:31 AM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57