• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic