使用安卓 Studio 中捆绑的 JDK 作为 Mac 上的 JAVA_HOME

我试着打开 Android 设备监视器,Studio 给我看了这条消息 = -O: need to install Java SE 6 runtime error

这让我很惊讶,因为如果我没有安装 Java,我怎么能够开发 Android 应用程序呢!事实上,Android Studio 附带了 JDK/JRE 绑定(位于 /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home中) ,但是系统没有找到它: 执行 usr/libexec/java_home给出

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

$JAVA_HOME设置为 /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home没有帮助ーー source .bash_profile不喜欢它是一个目录。

问: 如果我在 Android Studio 中已经有了一个 JDK,我不想安装新的 JDK。如何将其设置为系统默认值?

61046 次浏览

Solution:

  1. MacOS versions before Big Sur: add line export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home/ to .bash_profile file in your home directory. Run source ~/.bash_profile to update terminal.
  2. Catalina, Big Sur, Mentere and above: add line export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home to .zshenv file in your home directory. Run source ~/.zshenv to update terminal.

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.

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.

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.

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.

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/"

Here is how I solved my issue on MAC:

# Android, ADB and JAVA
export ANDROID_HOME="$HOME/Library/Android/sdk"
export PATH="$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$PATH"
export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/Contents/Home"