Add individual strings without "double" quotes..

                commands.add ( "C:\\Program Files\\CCBU\\CCBU.exe"                      );
                commands.add ( "-d");
                commands.add ("C:\\My Data\\projects\\ccbu\\ciccb-report.xls"        );
                commands.add ( "-tf");
                commands.add("C:\\Program Files\\CCBU\\loss-billing-filters.txt"   );
                commandExecutor = new SystemCommandExecutor(commands);

ProcessBuilder will take care of necessary handling of args.


Pull up comment:

Jayan, You're idea got me thinking : The following worked :

 commands.add ( "-dC:\\My Data\\projects\\ccbu\\ciccb-report.xls" );
 commands.add ( "-tfC:\\Program Files\\CCBU\\loss-billing-filters.txt"

); – lincolnadym

Answer from Jayan on Stack Overflow
Top answer
1 of 2
6

Add individual strings without "double" quotes..

                commands.add ( "C:\\Program Files\\CCBU\\CCBU.exe"                      );
                commands.add ( "-d");
                commands.add ("C:\\My Data\\projects\\ccbu\\ciccb-report.xls"        );
                commands.add ( "-tf");
                commands.add("C:\\Program Files\\CCBU\\loss-billing-filters.txt"   );
                commandExecutor = new SystemCommandExecutor(commands);

ProcessBuilder will take care of necessary handling of args.


Pull up comment:

Jayan, You're idea got me thinking : The following worked :

 commands.add ( "-dC:\\My Data\\projects\\ccbu\\ciccb-report.xls" );
 commands.add ( "-tfC:\\Program Files\\CCBU\\loss-billing-filters.txt"

); – lincolnadym

2 of 2
0

An old question, but I was running into similar behavior and wanted to share some background on what I found for this for anyone else like me who is looking.

Lack of argument separation in OS: Windows OS programs only take a single argument - CmdLine. The system calls for launching programs (at least the only ones I found -- WinExec and CreateProcess) do not, like on Linux, accept an array of arguments, but instead a string of CmdLine. This means that the program and the caller need to agree on how individual arguments are encoded - the OS doesn't make the separation of arguments part of the contract.

CmdLine convention: It seems from my experiments, that powershell, python, and more, agree that to pass separate arguments in CmdLine, they should be joined with a space, and if they contain space or double quote, they should be wrapped in double quotes with contained double quotes escaped by a backslash. This is good -- we can still pass any value as individual arguments.

ProcessBuilder flaw: ProcessBuilder accepts individual arguments. You would expect these to be passed to the subprocess as individual arguments in a way appropriate on the OS you are running. However, it seems it doesn't correctly do this when a double quote is within the argument on Windows. In my tests, it did not escape it. So if you have new ProcessBuilder("C:\\Program Files\\CCBU\\CCBU.exe", "-d\"my file\"", "-tf\"my other file\""), it ends up translating to a CmdLine value of "C:\Program Files\CCBU\CCBU.exe" "-d"my file"" "-tf"my other file"". At least dotnet seems to interpret this as having the arguments -dmy, file, -tfmy, other, file (i.e., values (quoted or not) next to other values (quoted or not) are treated as one argument, while spaces after even numbers of unescaped quotes are treated as separators.

Takeaway: Whenever you use ProcessBuilder with arguments containing double quotes, check if on Windows, and if so, add slashes before double quotes in each argument you pass.

Quotes in the middle of arguments: As you see in the above example, something like -d"hello world" can become a single value of -dhello world in some contexts, including CmdLine, bash, and Cmd. This is a mechanism to allow for spaces without having to quote the whole argument ("-dhello world" would act the same). However, in the ProcessBuilder context, there is no such handling of these quotes, and instead arguments are separated by being separate ProcessBuilder inputs. This means that the quote is treated as part of the argument (preserved as literal -d"hello world").

Takeaway: Only add double quotes in ProcessBuilder arguments if the program you are calling wants a value with double quotes in it. If they are there only to preserve whitespace during the invocation, omit them.

🌐
Coderanch
coderanch.com › t › 627514 › java › ProcessBuilder-incorrectly-processes-embedded-spaces
ProcessBuilder incorrectly processes embedded spaces within embedded quotes as required by Windows 7 (Java in General forum at Coderanch)
January 24, 2014 - What is particularly annoying about this, is that I can pass correctly a space embedded file name/directory name but not pass a command line argument of the for -I"C:\Pro ..... mentioned above. Example of file name path that works successfully (double \\ required by Java): "C:\\Software Development\\MyTesting\\VS2008\\CPP\\EchoCommandLineArguments\\Debug\\EchoCommandLineArguments.exe" Which ProcessBuilder builds process request as C:\Software Development\MyTesting\VS2008\CPP\EchoCommandLineArguments\Debug\EchoCommandLineArguments.exe" For the curious, EchoCommandLineArguments.exe is a quick and dirty C++ executable the echos its command line arguments.
🌐
Bug Database
bugs.java.com › bugdatabase › view_bug
Bug ID: JDK-7177083 ProcessBuilder does not handle command parameters with spaces correctly
FULL PRODUCT VERSION : java version ... with a space and ends with a wildcard (ie \"C:\\Documents and Settings\\\Java"*), ProcessImpl attempts to verify that the parameter is correctly quoted....
🌐
GitHub
github.com › eclipse › ceylon-sdk › issues › 172
Handle spaces in createProcess function · Issue #172 · eclipse-archived/ceylon-sdk
December 22, 2013 - It is currently not possible to create a process with parameters containing spaces signature of createProcess should be improved to accept String|[String+] instead of just String
Author   eclipse-archived
🌐
Baeldung
baeldung.com › home › java › core java › guide to java.lang.processbuilder api
Guide to java.lang.ProcessBuilder API | Baeldung
November 26, 2025 - The API automatically handles arguments containing spaces, so there’s no need to add quotes manually. If we instead tried to pass the entire command as a single string, it would fail because ProcessBuilder would interpret it as the executable ...
🌐
Stack Overflow
stackoverflow.com › questions › 23369786 › processbuilder-run-bat-file-with-arguments-spaces-in-path
java - ProcessBuilder - Run bat file with arguments - spaces in path - Stack Overflow
URI outputURI = new URI(("file:///"+ dir)); File f = new File(outputURI); for (final File fileEntry : f.listFiles()) { if(fileEntry.getName().equals("R.bat")){ dir = fileEntry.getAbsolutePath(); } } expr[0] = dir;//"\""+dir+"\""; expr[1] = "-e"; expr[2] = "\"library(Rserve);Rserve("+(debug?"TRUE":"FALSE")+",args='"+rsrvargs+"')\""; expr[3] = "--no-save"; expr[4] = "--slave"; ProcessBuilder pb = new ProcessBuilder(expr); pb.directory(f); p = pb.start(); ... returns a path without a space, everything works fine.
Find elsewhere
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8263697
Safer Process Launch by ProcessBuilder and Runtime.exec
The safer modes help applications apply the recommendations of the Secure Coding Guidelines for Java SE to avoid risks such as injection attacks and unintended execution. But most developers do not take advantage of the additional safety checks. Changing the default to be more secure can reduce the risk of unidentified execution and file access. We are changing the default for ProcessBuilder and Runtime.exec to require quoted arguments to have properly balanced quotes and to guard against splitting or merging of arguments.
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8190409
ProcessBuilder does not handle the spaces in the arguments
October 31, 2017 - ProcessBuilder API fails to handle the parameters if it contains space in it.
🌐
Blogger
guide-nea1l.blogspot.com › 2014 › 08 › windows-java-processbuilder-command.html
windows - Java - ProcessBuilder command arguments with spaces and double-quotes fails -
November 19, 2017 - if issue command , arguments indicated in following stackoverflow post (processbuilder adds quotes command line) string [] array fails, spaces in directory paths break arguments somehow ccbu.exe executable :
🌐
OpenJDK
openjdk.org › jeps › 8263697
JEP draft: Safer Process Launch by ProcessBuilder and Runtime.exec
For example, if an argument contains or may contain space or tab, applications find it appealing to add double-quotes around an argument. ProcessBuilder already handles the encoding of arguments with space and tab to provide some measure of portability across operating systems.
🌐
Stack Overflow
stackoverflow.com › questions › 66045955 › java-processbuilder-in-windows-spaces-in-exe-path-and-in-argument
java ProcessBuilder in Windows spaces in *.exe path and in argument - Stack Overflow
To make it work WITH cmd and WITH spaces you need to add yet another layer of quoting. After all you write a java program. The java compiler will expect strings to be quoted, but at runtime these quotes are no longer there.
🌐
Bug Database
bugs.java.com › bugdatabase › view_bug.do
Java
This bug is not available · More information is available here
🌐
Rip Tutorial
riptutorial.com › pitfall: runtime.exec, process and processbuilder don't understand shell syntax
Java Language Tutorial => Pitfall: Runtime.exec, Process and...
The problem is that exec(String) splits the string into a command and arguments by simply looking for whitespace. The command string: ... Faced with this, some programmers try to add quotes around the pathname. This doesn't work either: ... The extra double-quote characters that were added in attempt to "quote" the spaces are treated like any other non-whitespace characters.
🌐
Stack Overflow
stackoverflow.com › questions › 44420353 › java-processbuilder-error-when-spaces-in-path
Java ProcessBuilder Error When Space(s) In Path - Stack Overflow
June 8, 2017 - That's actually exactly what I did :/ Created a new File object with that path, then checked if the file is accessible by the code with myFile.exists() and then created the pBuilder like new ProcessBuilder(myFile.getAbsolutPath()) without any other arguments. ... I just tried this and it worked: ProcessBuilder pb = new ProcessBuilder("C:\\my dir with spaces\\whatever\\my.exe"); pb.start()