Solved

Runtime.exec issue with quotes

Posted on 2006-07-12
11
361 Views
Last Modified: 2012-05-05
Hi,

We are implementing a wrapper around a unix utility. It takes multiple parameters and we build the command line and execute from our java code. e.g. Here is what we are trying to execute

/opt/myunixutil/bin/myutil -to emailaddress -data datafilename -subject subject

When we try to pass subject with multiple worlds e.g. Test Data Prices in quotes

/opt/myunixutil/bin/myutil -to abc@cfg.com -data /tmp/datafile -subject 'Test Data Prices',

myutil actually throws back error 'unidentified option Data. So it does not recognize the single quote. If it is executed from command line, it works fine. If we try to change it to "", it does not work as well.

We are using Runtime.exec(String) call and String parameter is build with command mentioned earlier.

Can anyone help us out addressing this issue.

Thanks in advance


0
Comment
Question by:maha
[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
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 17094979
Use

String[] command = { "", "", ................., "-subject", "Test Data Prices" };
Runtime.getRuntime().exec(command);

(I'll let you fill in the blanks)
0
 
LVL 92

Expert Comment

by:objects
ID: 17095466
try escaping the quotes:

"/opt/myunixutil/bin/myutil -to abc@cfg.com -data /tmp/datafile -subject \"Test Data Prices\""
0
 
LVL 3

Expert Comment

by:mdg12
ID: 17096200
Which JRE are you using?   I had similar problems in JRE 1.3..  The only solution I was able to get working was to remove the spaces (and thus quotes) altogether..  You can use an escape characte, e.g.. subject.replace(' ','_') in java, and then similar unescape in your myutil
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 17097175
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17097821
0
 

Author Comment

by:maha
ID: 17100390
I am using JDK 1.4.2, SO I cannot use ProcessBuilder. I have already tried with escape character. I tried passing the values through String array ( recommended by CEHJ ), but it is giving me different error. It is saying,

/opt/myunixutil/bin/myutil -to abc@cfg.com -data /tmp/datafile -subject 'Test Data Prices' not found. It is not finding myutil utility binary.

About removing the spaces with _, I donot have any control over changing myutil.

If I have subject with single word, it works fine.

0
 
LVL 3

Expert Comment

by:mdg12
ID: 17100905
I'm convinced that there's no easy solution to this problem...  If you want to try escaping the parameters (which worked for me), but you cannot change myutil, you might consider writing a simple bash script that does the unescaping (with sed possibly) and calls myutil, passing along the arguments...  Then your java program would call the bash script with the escaped arguments

Good luck!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17102319
>>I tried passing the values through String array ( recommended by CEHJ ), but it is giving me different error

Can you post your code please?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17102329
... and please paste the error in too
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17146132
:-)
0
 

Author Comment

by:maha
ID: 17146337
I was trying to put the full command with all options as one command in the array. But I needed to break it up each option as a new command and it worked fine.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

717 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