macosunixsymlink

How to add and use Android Studio JRE or JBR in $PATH via symlink?


Coming from Windows, you can easily add Android Studio's JRE now JBR directory in environment variable path where executable such as jarsigner and keytool are located. I tried to do the same in macOS but using symlink as the suggested method I am seeing in the web.

First I copied the directory to my clipboard

/Applications/Android Studio Preview.app/Contents/jbr/Contents/Home/bin

Then followed this SO answer and executed the below command

sudo ln -s /Applications/Android Studio Preview.app/Contents/jbr/Contents/Home/bin /usr/local/bin

I already echoed $PATH and found usr/local/bin so I skipped the mkdir part. Unfortunately it does not work and seems broken.

This is what I am seeing

user@User-MacBook-Pro ~ % ls -la /usr/local/bin/
total 354896
drwxr-xr-x  9 root  wheel        288 Aug  9 02:31 .
drwxr-xr-x  6 root  wheel        192 Aug  8 01:12 ..
lrwxr-xr-x  1 root  wheel         21 Aug  9 02:31 Android -> /Applications/Android
lrwxr-xr-x  1 root  wheel          6 Aug  9 02:31 Studio -> Studio
lrwxr-xr-x  1 root  wheel         42 Aug  9 02:31 bin -> Preview.app/Contents/jbr/Contents/Home/bin
lrwxr-xr-x  1 root  wheel         45 Aug  8 02:39 corepack -> ../lib/node_modules/corepack/dist/corepack.js
-rwxr-xr-x  1 root  wheel  181706736 Jul 18 20:09 node
lrwxr-xr-x  1 root  wheel         38 Aug  8 02:39 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxr-xr-x  1 root  wheel         38 Aug  8 02:39 npx -> ../lib/node_modules/npm/bin/npx-cli.js

enter image description here

What went wrong and how to fix it?


Solution

  • After searching for hours I finally got what I am looking for from this SO answer

    First check if java_home is in /usr/libexec

    Then check if /Library/Java/JavaVirtualMachines directory exist

    After confirming, get or copy the Android Studio JBR path in my case this is /Applications/Android Studio Preview.app/Contents/jbr

    Noticed that there is spaces in path, what you can do is either add escape or just wrap the whole path with double quote.

    Be careful with the double quote " as copying from apps line Notes that has Smart Quote Substitution enabled, it will change your regular quotes to “ Left Double Quotation.

    Then you will have this command to create symlink instead of copy pasting it to JavaVirtualMachines folder.

    sudo ln -s "/Applications/Android Studio Preview.app/Contents/jbr" /Library/Java/JavaVirtualMachines
    

    Once success add this command in your ~/.zshrc

    export JAVA_HOME=$(/usr/libexec/java_home)
    

    You should now be able to run those commands that are available inside JRE or JBR bin such as keytools and jarsigner while also setting and fixing your JAVA_HOME setup.