Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

splitting string to arguments for Runtime.exec

Thread view: 
ittay.dror@gmail.com - 30 Mar 2005 08:56 GMT
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
Lisa - 30 Mar 2005 21:37 GMT
> Hi,
>
[quoted text clipped - 8 lines]
> Thanx,
> Ittay

try split
Alan Moore - 31 Mar 2005 02:05 GMT
>Can someone please post a code example of how to convert a string with
>execution command to array of arguments for Runtime.exec? (since
[quoted text clipped - 3 lines]
>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();


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.