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?

  • https://stackoverflow.com/questions/16639285/spaces-in-java-file-path-to-an-executable It works for you with `cmd` because you pass the job of formatting the string correctly to `cmd` itself. – Koenigsberg Feb 04 '21 at 12:52
  • it works wihout spaces in the arg only - but how to get it working WITH spaces? – BiggusBuggus Feb 04 '21 at 13:10

1 Answers1

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\""]
Queeg
  • 7,748
  • 1
  • 16
  • 42
  • 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` – BiggusBuggus Feb 09 '21 at 11:56
  • 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. – BiggusBuggus Feb 09 '21 at 18:59
  • 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. – Queeg Feb 12 '21 at 18:50
  • 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 -> https://stackoverflow.com/questions/32314645/java-processbuilder-command-arguments-with-spaces-and-double-quotes-fails \ (Edited) – Nor.Z May 07 '23 at 10:22
  • 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. – Queeg May 07 '23 at 18:31