Using JDK that is bundled inside Android Studio as JAVA_HOME on Mac

MacosAndroid StudioOpenjdkJava Home

Macos Problem Overview


I tried to open Android Device Monitor and the Studio showed me this message =-O : need to install Java SE 6 runtime error

It surprised me, because how have I been able to develop Android apps if I didn't have any Java installed?! Actually, Android Studio comes with bundled JDK/JRE (located in /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home), but it is not found by the system: executed usr/libexec/java_home gives

Unable to find any JVMs matching version "(null)".
No Java runtime present, try --request to install.

Setting $JAVA_HOME to /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home did not help — source .bash_profile doesn't like that it's a directory.

QUESTION: I don't want to install new JDK if I already have one inside Android Studio. How do I set it as system default?

Macos Solutions


Solution 1 - Macos

Add the correct string to .bash_profile (and reload with source .bash_profile):

  1. MacOS versions before Big Sur: export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home/.
  2. Catalina, Big Sur, Mentere and above: export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home

If you're using Catalina and above, you most probably use zsh as a shell instead of bash. In that case, add it to .zshrc or .zprofile instead of .bash_profile in your home directory.

Don't forget to restart the operating system after.

After that, running java -version gave this output and Java started to execute normally:

openjdk version "1.8.0_112-release"
OpenJDK Runtime Environment (build 1.8.0_112-release-b06)
OpenJDK 64-Bit Server VM (build 25.112-b06, mixed mode)

As for the Android Device Monitor — it still demands this ancient JRE version 6.

Solution 2 - Macos

I just did a fresh install of Android Studio Arctic Fox 2020.3.1 on Big Sur, and I had to use the following.

export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home/

On a slightly different topic, even with the correct JAVA_HOME, ~/Library/Android/sdk/tools/bin/sdkmanager wouldn't start. I had to install "Android SDK Command-line Tools (latest)" (from Android Studio) and use ~/Library/Android/sdk/cmdline-tools/latest/bin/sdkmanager instead.

Solution 3 - Macos

The core problem is that starting from Android Studio Arctic Fox the destination of bundled java has been changed from: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/ to: /Applications/Android Studio.app/Contents/jre/Contents/Home/

You just need to change JAVA_HOME env variable in .bash_profile to the new correct path (be careful with space character, easiest way just to specify path in doubled quotes). And either restart terminal or run source ~/.bash_profile.

Solution 4 - Macos

As pointed out by dimezis, JAVA_HOME might be working but the /user/libexec/java_home is still broken.

This is because in Mac, java_home and JAVA_HOME are different. JAVA_HOME is the environment variable / path that will be used in most applications and environment while java_home is the actual executable that is being used by the system to run java. Apparently, some Mac applications (like Xcode) will just ignore the JAVA_HOME path and make use of the java_home executable

So in order to use the Android Studio Embedded JDK as default Java to be used in Mac, copy the entire embedded jdk folder /Applications/Android Studio.app/Contents/jre/jdk to the default Java library folder /Library/Java/JavaVirtualMachines/

After this, set your .bash_profile or .zshrc with this: export JAVA_HOME=$(/usr/libexec/java_home)

This will make sure that your JAVA_HOME and java_home is pointing to the same java installation.

Solution 5 - Macos

in my case escaping with \ doesn't work but escaping whole path with " works fine. Please reopen terminal to see changes or run source ~/.zshenv to update terminal.

export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/Contents/Home/"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsoshialView Question on Stackoverflow
Solution 1 - MacossoshialView Answer on Stackoverflow
Solution 2 - MacossolamourView Answer on Stackoverflow
Solution 3 - MacosbusyleeView Answer on Stackoverflow
Solution 4 - MacoscrjacinroView Answer on Stackoverflow
Solution 5 - MacosYvgenView Answer on Stackoverflow