Executable name has embedded quote, split the arguments

JDK7u21更新后,部分应用程序使用Runtime.exec方法启动包含空格的命令时出现错误。本文介绍了解决此问题的方法,建议使用ProcessBuilder替代。

将JDK升级到7u21后,一个应用的内置Tomcat启动不了,报出 Executable name has embedded quote, split the arguments 的错误。

经过查询发现 jdk 7u21 和 jdk 6u 45的改变了Runtime.exec方法实现。对含有空格的命令会有影响。


Changes to Runtime.exec

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.

For example, Runtime.getRuntime().exec("C:\\My Programs\\foo.exe bar") is an attempt to launch the program "C:\\My" with the arguments "Programs\\foo.exe" and "bar". This command is likely to fail with an exception to indicate "C:\My" cannot be found.

The example Runtime.getRuntime().exec("\"C:\\My Programs\\foo.exe\" bar") is an attempt to launch the program "\"C:\\My". This command will fail with an exception to indicate the program has an embedded quote.

Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.

Alternatively, the preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.


参考资料

[1] Java™ SE Development Kit 6, Update 45 (JDK 6u45) Update Release Notes. http://www.oracle.com/technetwork/java/javase/6u45-relnotes-1932876.html
[2] Java™ SE Development Kit 7, Update 21 (JDK 7u21) Update Release Notes.  http://www.oracle.com/technetwork/java/javase/7u21-relnotes-1932873.html
[3] exec problem is JDK 1.7.0_21. http://www.velocityreviews.com/forums/t959814-exec-problem-is-jdk-1-7-0_21-a.html


<think>我们正在处理一个关于命令行参数错误的查询。用户遇到了错误信息:"error: the following arguments are required: executable_name, argv"。这通常发生在运行需要命令行参数的程序时,没有提供足够的参数。 根据用户提供的引用片段,虽然与错误没有直接关系,但提到了`ARG_MAX`(命令行参数的最大长度限制),但我们当前的问题是关于缺少必要的参数。 这个错误一般出现在使用类似Python的`argparse`库解析命令行参数时,如果定义了必需的参数但用户没有提供,就会抛出这样的错误。 解决方案的核心是确保运行程序时提供了所有必需的参数。具体步骤可能包括: 1. 检查程序的使用说明(通常通过`-h`或`--help`查看)。 2. 确保在运行命令时提供了所有标记为必需(required)的参数。 3. 检查参数的位置顺序是否正确(如果使用位置参数)。 例如,如果程序要求两个位置参数:`executable_name`和`argv`,那么运行命令时应该提供这两个参数,如: ```bash python my_program.py some_executable arg1 arg2 ... ``` 这里,`my_program.py`是当前运行的脚本,`some_executable`是第一个必需参数(对应`executable_name`),`arg1 arg2 ...`是第二个必需参数(对应`argv`,可能是一个列表,但传递时作为多个参数)。 如果用户是在Python的argparse中遇到这个错误,那么需要检查代码中是否正确定义了必需的参数,并且在运行程序时提供了这些参数。 下面我将按照步骤详细说明解决方案。 </think>### 解决 "error: the following arguments are required: executable_name, argv" 错误的方法 该错误通常发生在使用 Python 的 `argparse` 模块时,程序缺少必需的命令行参数。以下是完整的解决步骤: #### 1. **检查运行命令** 确保在执行命令时提供了所有必需参数: ```bash # 正确格式(假设程序需要两个参数) python your_script.py [executable_name] [argv] ``` ➤ 检查参数顺序是否正确,参数数量是否匹配程序要求 #### 2. **查看帮助文档** 使用 `-h/--help` 参数查看程序的使用说明: ```bash python your_script.py -h ``` 输出示例: ``` usage: script.py [-h] executable_name argv positional arguments: executable_name 程序名称 argv 参数列表 ``` ➤ 这会明确显示哪些参数是必需的[^1] #### 3. **修改代码设置默认值**(可选) 在 `argparse` 定义处为必需参数添加默认值: ```python import argparse parser = argparse.ArgumentParser() parser.add_argument('executable_name', nargs='?', default="default.exe") # 添加默认值 parser.add_argument('argv', nargs='*', default=[]) # 允许空列表 args = parser.parse_args() ``` ➤ 使用 `nargs='?'` 或 `nargs='*'` 使参数变为可选 #### 4. **检查参数定义** 确认代码中是否正确定义了必需参数: ```python # 错误示例(缺少必需参数定义) parser.add_argument('--optional_arg') # 缺少必需的位置参数 # 正确示例 parser.add_argument('executable_name') # 必需位置参数 parser.add_argument('argv', nargs='+') # 必需且至少一个值 ``` #### 5. **验证参数传递** 确保在 IDE 或脚本中运行时正确传递了参数: - 在 VSCode:检查 `launch.json` 的 `args` 字段 - 在 PyCharm:`Run > Edit Configurations > Parameters` #### 常见原因总结: | 原因 | 解决方案 | |------|----------| | 命令行缺少参数 | 补充缺失参数 | | 参数顺序错误 | 按帮助提示顺序输入 | | 代码未定义默认值 | 添加 `default` 或 `nargs` | | IDE 配置未传参 | 检查运行配置 |
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值