Solved

weird Runtime.exec() problem

Posted on 2002-06-05
2
186 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
ID: 7056580
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_
ID: 7057262
yep, already figured it out and did this - well answered anyway
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

685 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