Solved

Runtime.exec issue with quotes

Posted on 2006-07-12
11
343 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
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 74
groupSum5 challenge 5 85
how do i compare an object based on two fields 6 54
Java exception bubble up 2 18
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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

803 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