Solved

weird Runtime.exec() problem

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
expectj telnet failing 5 38
how to install java on RHEL image on EC2 4 31
Unhandled exception type Exception 18 31
jsp insert to database example 2 27
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

825 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