环境: mac
前提条件:我已经配置了 'adb'
到我的 bash_profile
的完整路径。当我尝试在终端中输入 'adb'
时,它正在工作。
但是,我尝试从 java 执行 'adb'
命令,'adb'
不起作用,而是我需要传递完整的 adb 路径才能使其工作.
我猜这可能与bash_profile
设置有关,有人知道这个问题的确切原因吗?
最佳答案
Runtime.getRuntime().exec()
运行/bin/sh -c <command>
。如果这是或指向您系统上的 bash shell:非交互式 bash 不会读取 .bash_profile
除非明确( --login
)告诉这样做。
来自documentation :
When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
这有点复杂,但非交互式、非登录 bash 实例不会读取配置文件。
子 shell 不会获取您的路径设置(实际上是 /bin/sh
,甚至可能根本不是 bash)。
如果需要,可以将路径添加到 adb
在系统范围内添加 appropriate entry to to /etc/paths.d
.
关于java - 运行 java exec 命令,总是需要应用程序的完整绝对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35766620/