Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

Runtime.exec issue with quotes

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
maha
Asked:
maha
  • 4
  • 2
  • 2
  • +3
1 Solution
 
CEHJCommented:
Use

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

(I'll let you fill in the blanks)
0
 
objectsCommented:
try escaping the quotes:

"/opt/myunixutil/bin/myutil -to abc@cfg.com -data /tmp/datafile -subject \"Test Data Prices\""
0
 
mdg12Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
girionisCommented:
0
 
Ajay-SinghCommented:
0
 
mahaAuthor Commented:
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
 
mdg12Commented:
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
 
CEHJCommented:
>>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
 
CEHJCommented:
... and please paste the error in too
0
 
CEHJCommented:
:-)
0
 
mahaAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now