简体   繁体   English

运行java exec命令,始终需要应用程序的完整绝对路径吗?

[英]run java exec command, always need full absolute path to the application?

Environment: mac 环境: mac

Precondition: I already configured 'adb' full path to my bash_profile . 前提条件:我已经为我的bash_profile配置了'adb'完整路径。 and when I tried type 'adb' in my terminal, it is working. 当我在终端中尝试输入'adb'时,它正在工作。

But, I tried to exec 'adb' command from java, 'adb' is not working, instead I need to pass the full adb path to make it work. 但是,我尝试从Java执行'adb'命令, 'adb'无法正常工作,相反,我需要传递完整的adb路径才能使其正常工作。

I guess this is probably something to do with the bash_profile setting, anyone know the exact reason for this issue? 我想这可能与bash_profile设置有关,有人知道此问题的确切原因吗?

Runtime.getRuntime().exec() runs /bin/sh -c <command> . Runtime.getRuntime().exec()运行/bin/sh -c <command> If this is or points to a bash shell on your system: A non-interactive bash does not read .bash_profile unless explicitly ( --login ) told to do so. 如果这是您的系统上的bash shell或指向该bash shell:非交互式bash不会读取.bash_profile除非明确要求( --login )这样做。

From the 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. 当Bash作为交互式登录shell或使用--login选项作为非交互式shell调用时,它首先从文件/ etc / profile中读取并执行命令(如果该文件存在)。 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. 读取该文件后,它将按该顺序查找〜/ .bash_profile,〜/ .bash_login和〜/ .profile,并从存在的且可读的第一个命令中读取并执行命令。 The --noprofile option may be used when the shell is started to inhibit this behavior. 启动外壳程序时,可以使用--noprofile选项禁止此行为。

It's a little convoluted, but non-interactive, non-login bash instances don't read the profile files. 这有点令人费解,但非交互式,非登录bash实例不会读取配置文件。

Your path settings does not get picked up by the subshell (which is actually /bin/sh which might not even be bash at all ). 子外壳程序(实际上是/bin/sh甚至根本不是bash )都不会占用您的路径设置。

If you want, you can add the path to adb system wide by adding an appropriate entry to to /etc/paths.d . 如果需要,可以通过/etc/paths.d添加适当的条目来将路径添加到adb系统范围/etc/paths.d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM