Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

java ProcessBuilder in Windows spaces in *.exe path and in argument

often discussed, but this seems a weired edge case.

In win cmd.exe I successfully run:

"c:Program Filesmyapp.exe" -my_arg="sth. with space"

and

"c:Program Filesmyapp.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 Filesmyapp.exe", "-my_arg=sth. with space"]         // no extra quoting
["cmd.exe", "/c", ""c:Program Filesmyapp.exe"", "-my_arg=sth. with space"]     // exe       quoted
["cmd.exe", "/c", ""c:Program Filesmyapp.exe"", "-my_arg="sth. with space""] // exe & arg quoted
["cmd.exe", "/c", "c:Program Filesmyapp.exe", "-my_arg="sth. with space""]     //       arg quoted

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

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

running this works fine:

["cmd.exe", "/c", "c:Program Filesmyapp.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?

question from:https://stackoverflow.com/questions/66045955/java-processbuilder-in-windows-spaces-in-exe-path-and-in-argument

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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""]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...