简体   繁体   English

java ProcessBuilder 在 Windows *.exe 路径和参数中的空格

[英]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:在 win cmd.exe 我成功运行:

"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):在 java ProcessBuilder.command(xxx) 以下失败,“c:\Program”不是有效命令(xxx 包含以下数组):

// 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.当 *.exe 路径和 arg 包含空格时,问题似乎就开始了。

[edit]: My question is: How can you run it with whitespaces in the exe's path AND in the arg's content? [编辑]:我的问题是:如何在 exe 的路径和 arg 的内容中使用空格运行它?

To make it work WITH cmd and WITH spaces you need to add yet another layer of quoting.要使其与 cmd 和空格一起使用,您需要添加另一层引用。

After all you write a java program.毕竟你写了一个 java 程序。 The java compiler will expect strings to be quoted, but at runtime these quotes are no longer there. java 编译器将期望字符串被引用,但在运行时这些引号不再存在。 Some of the strings will be used to start cmd, others will be passed on to cmd.一些字符串将用于启动 cmd,其他字符串将传递给 cmd。

Cmd itself checks the arguments it received and will parse them. Cmd 本身会检查它收到的 arguments 并将解析它们。 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. Cmd 将理解这些引号并删除它们 - 被调用的程序不再注意到它们。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有动态路径的ProcessBuilder(可能带有空格) - ProcessBuilder with dynamic path (possibly with spaces) 如何在Windows中使用Java中的processBuilder来执行带有参数的exe命令 - How to use processBuilder in Java to execute an exe with arguments in Windows Command Prompt 带有多个带空格的参数的Java ProcessBuilder - Java ProcessBuilder with multiple params with spaces ProcessBuilder 执行 java.exe - ProcessBuilder execute java.exe 如何在Windows中的ProcessBuilder java中设置PATH环境变量 - How to set PATH environment variable in ProcessBuilder java in windows ProcessBuilder无法在路径中运行带空格的bat文件 - ProcessBuilder cannot run bat file with spaces in path 如何使用ProcessBuilder在Java中使用空格启动快捷方式 - How to launch a Shortcut with Spaces in Java using ProcessBuilder 使用Java ProcessBuilder运行具有多个参数的Windows .exe文件不会产生任何预期的输出文件 - Running Windows .exe file with multiple arguments using Java ProcessBuilder is not producing any output file as expected 使用Java ProcessBuilder运行本机Windows .exe会产生错误::目录名称无效 - Using Java ProcessBuilder to run native Windows .exe producing error :: The directory name is invalid Java ProcessBuilder和Windows系统变量 - Java ProcessBuilder and Windows system variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM