troubleshooting Question

Java 6 exec, escaping special characters within ProcessBuilder.command(String[])

Avatar of numtech
numtech asked on
JavaShell Scripting
12 Comments1 Solution4411 ViewsLast Modified:

I use the commandline from my main java app to execute another 'mailer.jar' app.
One of the argument is the mail content that can contains any character including \n, \r, ", '...

I thought using

Open in new window

instead of  

Open in new window

would prevent the ProcessBuilder.exec from having troubles with splitting the command line, but it does not work.

If my command is a String[] array containing:
String[] cmd=new String[]{"java",
"my subject ...",
"my content with carriage returns and other strange characters"};

Open in new window

I will obtain as the String[] args of the main method of my mailer.jar
String[] args=new String[]{
"my subject ...",
"my content with",
"carriage returns and",
"other strange characters",

Open in new window

What should I do?
I would prefer not to use Apache Commons Exec because we have developped our own low level lib to handle multi threading and asynchronous exec...

Thanks for your help!



Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros