3

often discussed, but this seems a weired edge case.

In win cmd.exe I successfully run:

"c:\Program Files\myapp.exe" -my_arg="sth. with space"

and

"c:\Program Files\myapp.exe" -my_arg="sth_without_space"

in java ProcessBuilder.command(xxx) following fails with "c:\Program" was not a valid command (xxx contains following array):

// using cmd.exe:
["cmd.exe", "/c", "c:\Program Files\myapp.exe", "-my_arg=sth. with space"]         // no extra quoting
["cmd.exe", "/c", "\"c:\Program Files\myapp.exe\"", "-my_arg=sth. with space"]     // exe       quoted
["cmd.exe", "/c", "\"c:\Program Files\myapp.exe\"", "-my_arg=\"sth. with space\""] // exe & arg quoted
["cmd.exe", "/c", "c:\Program Files\myapp.exe", "-my_arg=\"sth. with space\""]     //       arg quoted

// putting all as cmd.exe arg:
["cmd.exe", "/c", "c:\Program Files\myapp.exe -my_arg=sth. with space"]            // no extra quoting
["cmd.exe", "/c", "\"c:\Program Files\myapp.exe\" -my_arg=sth. with space"]        // exe       quoted
["cmd.exe", "/c", "\"c:\Program Files\myapp.exe\" -my_arg=\"sth. with space\""]    // exe & arg quoted
["cmd.exe", "/c", "c:\Program Files\myapp.exe -my_arg=\"sth. with space\""]        //       arg quoted

// calling *.exe directly
["c:\Program Files\myapp.exe", "-my_arg=sth. with space"]                          // no extra quoting
["\"c:\Program Files\myapp.exe\"", "-my_arg=sth. with space"]                      // exe       quoted
["\"c:\Program Files\myapp.exe\"", "-my_arg=\"sth. with space\""]                  // exe & arg quoted
["c:\Program Files\myapp.exe", "-my_arg=\"sth. with space\""]                      //       arg quoted

running this works fine:

["cmd.exe", "/c", "c:\Program Files\myapp.exe", "-my_arg=sth_without_space"]

The issues seem to start when the *.exe path and the arg contain whitespaces.

[edit]: My question is: How can you run it with whitespaces in the exe's path AND in the arg's content?

2
  • stackoverflow.com/questions/16639285/… It works for you with cmd because you pass the job of formatting the string correctly to cmd itself. Commented Feb 4, 2021 at 12:52
  • it works wihout spaces in the arg only - but how to get it working WITH spaces? Commented Feb 4, 2021 at 13:10

1 Answer 1

0

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. Some of the strings will be used to start cmd, others will be passed on to cmd.

Cmd itself checks the arguments it received and will parse them. To markup which whitespace is a delimiter and which is not you need to have quotes. Cmd will understand these quotes and remove them - the called program does not notice them any more.

So either add more quotes (with correct escaping) or try to run the executable directly.

["cmd.exe", "/c", "\"c:\\Program Files\\myapp.exe\"", "\"-my_arg=sth_with space\""]
Sign up to request clarification or add additional context in comments.

5 Comments

did this workout on your machine? On mine it responds: 'c:\Program' is not recognized as an internal or external command, operable program or batch file. running cmd -h reponds: Microsoft Windows [Version 10.0.18363.1256] (c) 2019 Microsoft Corporation. All rights reserved. and my java is: openjdk version "14.0.2" 2020-07-14
I just came across this: you can directly run the ["*.exe"] without putting ["cmd.exe", "/c"] upfront - even without quoting at all. BUT: this will work for *.exe only. it does not work if you target to also run foo.bat and pass args with spaces to it.
I figured out you also want to escape the backslashes so the java compiler does not have to complain (interesting that it did not for you). If you insist it still complains that C:/Program is not found, you need to quote yet another time.
I think Java Process cmd Parsing is not implemented well. \ 1. even with quotes it doesnt work in my case. \ 1. in some other cases quotes in ProcessBuilder can even cause trouble -> stackoverflow.com/questions/32314645/… \ (Edited)
The system is working well. But you need to know which layers need which layers of escaping, and this becomes quite quickly complex and error-prone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.