Where does Java's ProcessBuilder look to execute commands?

前端 未结 2 1925
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 19:32

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

相关标签:
2条回答
  • 2020-12-11 20:21

    If you want to take control of finding commands, then, well, take control of finding commands. Don't let ProcessBuilder search. Use your own code to find what you want to run, and then put an absolute pathname into the parameter to ProcessBuilder.

    0 讨论(0)
  • 2020-12-11 20:26

    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.

    0 讨论(0)
提交回复
热议问题