1

I would like to run an app from my service using the java processBuilder class however that app has command line arguments that have values that have dashes in them.

e.g. >app -b 8 -e u-law

when I create a processBuilder instance and pass in "-e u-law" as one of the command arguments, it can't seem to handle the dash in the argument value. Is there a way around this to get it working?

2
  • you need to escape the dash? Commented Aug 12, 2014 at 21:12
  • @charlesbabbage: no. Rather he needs to separate the tokens in the command String. Commented Aug 12, 2014 at 21:25

1 Answer 1

3

You need to add each String token, that is, each command sub-String that is separated by white space, separately to the ProcessBuilder's command parameter array or List. For this command line,

e.g. >app -b 8 -e u-law

You have 5 String tokens, app, -b, 8, -e, and u-law (since you have no gap between the u and the dash). You could then do something like this:

List<String> list = new ArrayList<>();
list.add("app"); 
list.add("-b");
list.add("8"); 
list.add("-e"); 
list.add("u-law");
ProcessBuilder pBuilder = new ProcessBuilder(list);

alternatively you could use an array of 5 Strings for this,

String[] commands = {"app", "-b", "8", "-e", "u-law"};
ProcessBuilder pBuilder = new ProcessBuilder(commands);
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent!!! Thanks for such a quick response, much appreciated. What I was missing was that every string had to be a separate entry. For arguments like: -c 1 .... I thought I could put that in one string entry: "-c 1" and same goes for the other value that had a dash in it: -e u-law ... I even tried separate strings for that one, but missed the separate strings for the other ones where only space is involved. It works now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.