java - 运行 java exec 命令,总是需要应用程序的完整绝对路径?

标签 java macos bash

环境: 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/

相关文章:

linux - Bash/Shell-将所有文件从子目录 move 到目标目录?

java - Java 中的动态方法绑定(bind)

java - 在不使用 java.awt.Color 的情况下从 HSV(Java 中的 HSB)转换为 RGB(Google App Engine 不允许)

java - 如何将 ArrayList 内的对象实例与通过 Scanner 的用户输入进行比较

mysql - 新安装后缺少 mysql.sock

swift - Swift 中的相互非可选引用循环

java - jsp自定义标签: doAfterBody method is not invoked

ios - 为什么 `init` 不能用在 `NSCalendar *cal = [[NSCalendar alloc] init]` 中?

linux - 使用 sed 或 awk 或新安装的 Debian 9 中的工具,如何添加未修剪的文本?

linux - 如何使用 tty 通过 ssh 运行 screen 执行命令