I would like to pass multiple parameters to a processBuilder and the parameters to be separated by a space.
Here is the command,
String[] command_ary = {dir+"library/crc"," -s ", fileName," ",addressRanges};
I need to provide a space after "fcrc" and after "-p" and in between "filename" and the "addressRange".
Thank you
You don't need to include spaces. The ProcessBuilder will deal with that for you. Just pass in your arguments one by one, without space:
We need spaces between arguments in commandline because the commandline need to know which is the first argument, which is the second and so on. However when we use
ProcessBuilder
, we can pass an array to it, so we do not need to add those spaces to differentiate the arguments. The ProcessBuilder will directly pass the command array to theexec
after some checking. For example,The above code will work perfectly.
Moreover, you can use
But it is more convenient to use ProcessBuilder, one reason is that if our argument contains space we need to pass quote in
Runtime.getRuntime().exec()
likejava -cp C:/testt \"argument with space\"
, but with ProcessBuilder we can get rid of it.Use it like this:
new java.lang.ProcessBuilder('netstat -an'.toString().split('\\s'))).start()