SDS
SDS

Reputation: 3

Need to pass parameters with spaces to a command wrapped in batch file from java

I need to pass parameters with spaces to a command wrapped in batch file. I need to trigger the same from my code in java.

I have tried to pass the parameters in double quotes but still it is taking the value inside the quotes which is with spaces and hence couldn't execute the command. The command wrapped in batch file is - msend -n {server} -a {event} -b "mc_object_class=%par1%;mc_parameter=%par2%;support_group=%par3%"

The java method I have used to trigger the same is - Process process = new ProcessBuilder("C:/test.bat", "\""+object_class+"\"", "\""+mc_parameter+"\"", "\""+support_grp+"\"").start();

Parameters with quotes and without quotes are treated as same from java, whereas from command prompt it works fine with double quotes. Need to know the way to pass parameters with spaces from ProcessBuilder method.

Runtime.getRuntime().exec() method also giving the same error. My work is stuck at this point.

Any help will be highly appreciated.

Upvotes: 0

Views: 593

Answers (1)

MC ND
MC ND

Reputation: 70923

I don't know if inside your batch file you are changing the arguments. For this answer, I will directly use the input arguments (%1 to %3), but removing the double quotes (%~1 to %~3) that you need to pass from java code (so the arguments are properly tokenized), but that we don't need in the msend arguments.

But then the problem is that the spaces are not properly handled in the command. The msend documentation states that arguments with spaces or punctuation marks in a text field need single quotes inside the double quoted argument.

msend -n {server} -a {event} -b "mc_object_class='%~1';mc_parameter='%~2';support_group='%~3'"
                                                 ^...^              ^...^               ^...^
                                ^............................................................^

Upvotes: 1

Related Questions