问题描述
当我使用 ProcessBuilder
执行命令时,它如何知道在哪里查找该命令?使用 这个 hack/trick 我修改了我的 PATH 变量(通过检查 processBuilder.environment()
验证)是坏的(空的、工作目录等)但 ProcessBuilder 仍然可以执行排序、回声、bash 等.这是怎么做到的?!
When I execute a command using ProcessBuilder
, how does it know where to look for that command? Using this hack/trick I've modified my PATH variable (verified by inspecting processBuilder.environment()
) to be bad (empty, working dir, etc) but ProcessBuilder can still execute sort, echo, bash, etc. just fine. How is it doing this?!
注意:我的特定开发环境是 OSX,但此代码也可在 Red Hat Enterprise Linux 上运行.
Note: My particular development environment is OSX but this code will also run on Red Hat Enterprise Linux.
推荐答案
文档说
[...] 一个命令,一个字符串列表,表示要调用的外部程序文件及其参数,如果有的话.哪些字符串列表表示有效的操作系统命令取决于系统.[...]
这实质上意味着它寻找程序执行的位置取决于您正在运行的特定系统和 JVM.
Which in essence mean that where it looks for programs to execute depends on the particular system and JVM you're running on.
我找不到完整的 JVM/系统行为矩阵,但据推测它的行为类似于系统的流行 shell(bash
用于 *nix 和 cmd
对于 Windows),即它从左到右搜索 PATH
环境变量中的目录并执行它找到的第一个可执行文件.
I can't find a complete matrix of JVM / System behaviors, but supposedly it behaves similar to the popular shells of the system (bash
for *nix and cmd
for windows) i.e. it searches the directories in the PATH
environment variable from left to right and executes the first executable file it finds.
这篇关于Java 的 ProcessBuilder 在哪里执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!