Solved

weird Runtime.exec() problem

Posted on 2002-06-05
2
188 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Custom Java Application connects to database: Which one of the 20? 4 55
jboss 7.1 start up error 1 82
ejb message driven bean mdb creation steps 2 53
swing controls 2 37
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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.
Suggested Courses

737 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