Solved

Runtime.exec issue with quotes

Posted on 2006-07-12
11
324 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
 
LVL 35

Expert Comment

by:girionis
ID: 17097175
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17097821
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now