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

    Noticed that there is a whitespace in path, add escape or just wrap the whole path with double quote, see the below command sample.

    Be careful with the double quote " as copy pasting from apps like macOS Notes which has Smart Quote Substitution feature enabled will change a regular quotes to “ Left Double Quotation so turn it off when copy pasting text with double quotes.

    Instead of duplicating Android Studio's Embedded JDK/JBR folders and files to /Library/Java/JavaVirtualMachines, we will create a Symbolic Link. This link points to Android Studio's JDK/JBR, eliminating the need to copy everything whenever Android Studio updates.

    Note that Symbolic Links differ from Shortcuts. Shortcuts are actual files requiring specialized programs (GUI like Aqua or Windows shell) that knows how to handle such file in order to function.

    Open the terminal and execute the below command to create symlink of Android Studio's JDK/JBR to /Library/Java/JavaVirtualMachines.

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

    Add the JAVA_HOME environment by opening ~/.zshrc or ~/.zprofile in text editor and add the following line.

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

    Save and close the file.

    Restart your terminal session for the changes to take effect.

    Now you have a proper Java installation on macOS using Android Studio's JDK/JBR. You should now be able to run commands available inside the JBR bin such as keytools, jarsigner, etc.