Solved

Runtime.exec issue with quotes

Posted on 2006-07-12
11
355 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What's wrong with this code? 4 36
pagenation logic how it is working in my code 1 71
Eclipse Java import and method not resolved 4 82
Coding for the first time 9 70
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

730 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