• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Bhay Zone
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
author & internet detective
Marshal
Posts: 34651
365
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bhay Zone
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic