Solved

weird Runtime.exec() problem

Posted on 2002-06-05
2
182 Views
Last Modified: 2010-03-31
I need to run a command, basically:

Runtime rt = Runtime.getRuntime();

command = "dostuff -a blah -d \"blah blah\"";

Process p = rt.exec(command);
p.waitFor();

the second argument is the problem, for some reason the application treats the quoted argument as two separate ones and comlains that 'blah"' does not start with a '-'

if I echo this exact command and then copy and paste it from the shell, it works fine

any suggestions?
0
Comment
Question by:_D_
2 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 200 total points
Comment Utility
The single String exec method par5ses the command argument into tokens using a StringTokenizer with the default character delimiters which are the whitespace characters including blank. It does not recognize or honor quoting. What you will need to do is construct a String[] with each array element being one arg value. Something like:

    String[] cmdArray = new String[5];
    int argn = 0;
    cmdArray[argn++] = "dostuff";
    cmdArray[argn++] = "-a";
    cmdArray[argn++] = "blah";
    cmdArray[argn++] = "-d";
    cmdArray[argn++] = "blah blah";
    rt.exec(cmdArray);

Best regards,
Jim Cakalic
0
 

Author Comment

by:_D_
Comment Utility
yep, already figured it out and did this - well answered anyway
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now