Java的ProcessBuilder在哪里执行命令? [英] Where does Java's ProcessBuilder look to execute commands?
问题描述
当我使用 ProcessBuilder
执行命令时,它如何知道在哪里查找该命令?使用这个黑客/技巧我修改了我的PATH变量(通过检查 processBuilder.environment()
验证为坏(空,工作目录等),但ProcessBuilder仍然可以执行排序,echo,bash等等。
注意:我的特定开发环境是OSX,但是这个代码也可以在Red Hat Enterprise Linux上运行。
表示要调用的外部程序文件的字符串列表及其参数(如果有)。哪些字符串列表表示有效的操作系统命令是系统相关的。 [...]
这本质上意味着在哪里寻找程序执行取决于特定的系统和JVM运行。
我找不到一个完整的JVM / System行为矩阵,但是它的行为类似于系统的流行shell( bash
代表* nix和 cmd
代表窗口),即它搜索 PATH
环境变量从左到右并执行它找到的第一个可执行文件。
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?!
Note: My particular development environment is OSX but this code will also run on Red Hat Enterprise Linux.
The documentation says
[...] a command, a list of strings which signifies the external program file to be invoked and its arguments, if any. Which string lists represent a valid operating system command is system-dependent. [...]
Which in essence mean that where it looks for programs to execute depends on the particular system and JVM you're running on.
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在哪里执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!