Reputation: 2241
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.
Upvotes: 3
Views: 1835
Reputation: 2671
Try to use the process builder and / or seperate your arguments
Process Builder
ProcessBuilder pb = new ProcessBuilder("\"C:\\Program Files (x86)\\otherProg\\prog.exe\"", "/dothis ", "from + " " + to + " \"" + outputFile + "\"");
Process p = pb.start();
Runtime
Runtime.getRuntime().exec(new String[]{"\"C:\\Program Files (x86)\\otherProg\\prog.exe\"", "/dothis ", "from + " " + to + " \"" + outputFile + "\""});
Upvotes: 1
Reputation: 522151
I am posting this mainly for informational purposes. Consider the following code:
Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe echo Hello World");
This will indeed cause a Command Prompt to appear at my home (default) directory, but it will not actually execute the echo
command. In fact, the following will do the same:
Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe blah blah blah");
So it seems that Java is ignoring everything which comes after the cmd.exe
. My explanation for this is that the actual command is start
, and the parameter to that command is cmd.exe
. In other words, once Java has launched the Command Prompt, it has already used the parameter, and everything else is ignored.
This observation is consistent with your findings that the following works:
Process p = Runtime.getRuntime().exec(
"\"C:\\Program Files (x86)\\otherProg\\prog.exe\" /dothis 0 100 \"C:\\Program Files\\myProgram\\file.txt\"");
In this case, the command is prog.exe
, and what comes after it are the parameters. However, if you had tried piping your command into Command Prompt and running from there, then it would not have worked.
So it seems that using Runtime.getRuntime().exec()
allows you to execute one process from Java, but not two of them. This makes sense, since Java can execute a process, but the API does not allow it to launch a second process from the first one.
Upvotes: 4