简体   繁体   中英

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

Environment: mac

Precondition: I already configured 'adb' full path to my bash_profile . and when I tried type 'adb' in my terminal, it is working.

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.

I guess this is probably something to do with the bash_profile setting, anyone know the exact reason for this issue?

Runtime.getRuntime().exec() runs /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.

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. 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.

It's a little convoluted, but non-interactive, non-login bash instances don't read the profile files.

Your path settings does not get picked up by the subshell (which is actually /bin/sh which might not even be bash at all ).

If you want, you can add the path to adb system wide by adding an appropriate entry to to /etc/paths.d .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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