?
Solved

weird Runtime.exec() problem

Posted on 2002-06-05
2
Medium Priority
?
190 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 800 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 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