gpt4 book ai didi

Java语法转义符号?

转载 作者:行者123 更新时间:2023-12-01 13:39:07 25 4
gpt4 key购买 nike

我需要在 cmd 中处理命令,该命令如下所示:

"c:\Program Files (x86)\HMA! Pro VPN\bin\HMA! Pro VPN.exe" -changeip

但我无法真正添加 " 因为我会收到错误..有没有办法做到这一点?我尝试过的方法会导致错误:

cmd.exec(""c:/Program Files (x86)/HMA! Pro VPN/bin/HMA! Pro VPN.exe" -reconnect");

如何转义该字符才能使其正常工作?

Exception in thread "Thread-1" java.lang.IllegalArgumentException: Executable name has embedded quote, split the arguments
at java.lang.ProcessImpl.isQuoted(Unknown Source)
at java.lang.ProcessImpl.getExecutablePath(Unknown Source)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at HMACommand.reconnect(HMACommand.java:15)

最佳答案

您可以使用反斜杠 (\) 转义字符。

在您的情况下,结果将是这样的:

String test = "\"c:\\Program Files (x86)\\HMA! Pro VPN\\bin\\HMA! Pro VPN.exe\" -changeip";

您还必须转义反斜杠本身。

引用您的编辑:

This answer解释了为什么会出现错误。

来自the cited source :

On Windows platform, the decoding of command strings specified to Runtime.exec(String), Runtime.exec(String,String[]) and Runtime.exec(String,String[],File) methods, has been improved to follow the specification more closely. This may cause problems for applications that are using one or more of these methods with commands that contain spaces in the program name, or are invoking these methods with commands that are not quoted correctly.

相反,请使用ProcessBuilder

关于Java语法转义符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20981224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com