Im trying to execute a Process
with multiple parameters but they have double quotes "..."
.
This is how I build the script:
public void capture(String from, String to, String outputFile)
This method will run the command, it takes the 3 parameters which are given here:
capture("0", "100", "C:\\Program Files\\myProgram\\file.txt")
So the full built command looks like this:
String command = "\"C:\\Program Files (x86)\\otherProg\\prog.exe\" /dothis "
+ from + " " + to + " \"" + outputFile + "\"";
To see it clearly, this is the visual output of the command:
"C:\Program Files (x86)\otherProg\prog.exe" /dothis 0 100 "C:\Program Files\myProgram\file.txt"
Ok, and then I execute it like this:
String[] script = {"cmd.exe", "/c", command};
Process p = Runtime.getRuntime().exec(script);
And at this point nothing happens.
The command doesnt get executed, however if I take the output:
"C:\Program Files (x86)\otherProg\prog.exe" /dothis 0 100 "C:\Program Files\myProgram\file.txt"
Copy-paste it in CMD the command DOES get executed (and i get the expected output).
I have tried building the command like this but same effect happens.
The only possible way to run that command is to do it like this:
"C:\Program Files (x86)\otherProg\prog.exe" /dothis 0 100 C:\Folder\myProgram\file.txt
without the quotes on last parameter and of course, without spaces in the route.
What is the solution to this?
Update 1:
Also tried script = script.replace("\n","").replace("\t","")
and neither works.
Update 2:
Just tried building the process like this:
Process p = Runtime.getRuntime().exec(
"\"C:\\Program Files (x86)\\otherProg\\prog.exe\" /dothis 0 100 \"C:\\Program Files\\myProgram\\file.txt\"");
Passing the escaped command straight to the process does work, but why doesnt it work when they are parameters and building string with them?
SOLVED THANKS TO Tim Biegeleisen below
As he mentioned, there is a problem for java to make a difference between command and parameter and when to run multiple commands, to solve this do the next:
String command = "cd \"C:\\Program Files (x86)\\otherProgram\\\" & program.exe /capture "+from+" "+to+" \""+outputFile+"\"";
&
does it work.