File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Runtime.exec - passing String Command Line Parameters containing double quotes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.exec - passing String Command Line Parameters containing double quotes" Watch "Runtime.exec - passing String Command Line Parameters containing double quotes" New topic
Author

Runtime.exec - passing String Command Line Parameters containing double quotes

Bhay Zone
Greenhorn

Joined: Jul 13, 2009
Posts: 7

Help !!!

I am trying to invoke a command from my java program running under Redhat Linux using Java 1.6. The command i'm trying to execute is actually a shell script that executes a query on a remote system (GNATS - a bug tracking tool).

I need to execute the following command through my java program.
query-pr --host mygnatsdb.dom.net --port 1568 --expr '(last-modified > "2009-06-15")'

When I run this command directly at the command prompt, it executes fine.
However, if I run it from java, the tool complains that the query expression is Invalid. Somehow its not able to parse the expression passed in the last argument.

1] Escaping the quotes does not work. Same results.

2] I tried double escaping them (--expr '(last-modified > \\\"2009-06-15\\\")') thinking that java is doing some sort of parsing again before actually invoking my command, but that ended in the same result.

3] I cannot use single quotes as they are being used at the begingin to mark the start and end of the query expression, and switching between them is not allowed.

4] I tried using the exec(String[] cmdarray) method, however I'm not sure if I used it correctly, because it did not work. Tried several variations.
String cmdarr[] = {"query-pr", "--host", "spyro.juniper.net", "--port", "1528", --expr '(last-modified > \"2009-06-15\")};
Runtime.exec(cmdarray).

Now I have reached my wits end and can't seem to make any progress. Please help !!!

Bhay.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29219
    
134

Bhay,
The string array is the approach that should work.

Is this exactly what you tried? (It's missing quotes around the --expr" parameter and the parameter/value should be separate arguments.) What error did you get with this approach?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bhay Zone
Greenhorn

Joined: Jul 13, 2009
Posts: 7
Hi Jeanne,

Apologize for replying so late, and that too for my own query. Sorry again.

I figured out the problem and you were right, the string array is the right approach. However what I was doing wrong in my failed string array attempt was that I declared the expression and its flag as a single array element "--expr (last-modified > "2009-06-15\". The right way to do it is to split that into two separate element i.e. {"query-pr", " ...", "...", "--expr", "(last-modified > "2009-06-15\")"}.

Thanks a lot for your help.


Jeanne Boyarsky wrote:Bhay,
The string array is the approach that should work.

Is this exactly what you tried? (It's missing quotes around the --expr" parameter and the parameter/value should be separate arguments.) What error did you get with this approach?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Runtime.exec - passing String Command Line Parameters containing double quotes
 
Similar Threads
Calling a script on Linux from a JSP or servlet
runtime.exec problems
JDK malfunction
running oggenc from a java app
Open a command prompt using Runtime.getRuntime().exec() and run a jar file