129

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?

11 Answers 11

253

Solution:

  1. (Updated 2023) For Android Studio before Electric Eel 2022.1.1 and above: export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home
  2. 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.
  3. 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.

15
  • 2
    how to get keytool when using the OpenJDK? It says "To use the keytool command-line tool you need to install a JDK"
    – onexf
    Sep 18, 2017 at 12:55
  • 2
    Very useful. You cannot download JDK 8 from Oracle's site without a user. Only the latest JDK, 12 at this time - which does not work with gradle 4.4.1 which i'm using for my project. It's a mess. Also homebrew has no JDK 8. This was the solution that worked, thank god for embedded JDK! May 14, 2019 at 14:30
  • 3
    Sweet. I didn't have to restart my mac.
    – Aba
    Dec 4, 2019 at 10:10
  • 8
    For zsh the following line (added to the ~/.zshrc file) worked for me: export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/" Apr 13, 2021 at 18:38
  • 3
    With the latest "Electric Eel" release, /jre/ changed to /jbr/so it's now export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home Jan 19 at 14:18
101

Updated 2023

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

Android Studio Dolphin and below:

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

Android Studio Electric Eel 2022.1.1 and above:

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

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

7
  • Thank you solamour, I also just fresh install Android Studio Arctic Fox on Big Sur. And it work Aug 17, 2021 at 3:22
  • 2
    "/Applications/Android\ Studio.app/Contents/jre/Contents/Home/" it's not working.
    – Nelu B
    Sep 6, 2021 at 19:31
  • @NeluB, what is the output of echo $JAVA_HOME and which java, and java --version on your machine? While you are at it, share the output of ls -la /Applications/Android\ Studio.app/Contents/jre/Contents/Home/ as well.
    – solamour
    Sep 7, 2021 at 0:23
  • 2
    I fixed this by creating a symbolic link from the JRE folder to a new JDK folder. cd /Applications/Android\ Studio.app/Contents/jre ln -s ../jre jdk. Then updating back the JAVA_HOME to /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home. Thanks to fluttercorner.com/…
    – Nelu B
    Sep 7, 2021 at 15:38
  • This worked for me; I needed to accept the license agreements, but the sdkmanager suggested by Android Studio did not work. "Android SDK Command-line Tools (latest)" are installed from Preferences/Appearance & Behavior/System Settings/Android SDK, then "SDK Tools" tab, then check the box and click Apply. To accept the licenses, run the "latest" tool with the --licenses flag
    – nachbar
    Nov 9, 2021 at 4:06
14

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.

1
  • Thank you! It worked! I just need to close terminals and open new terminals to check.
    – Woppi
    Feb 22, 2022 at 15:02
10

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.

3
  • 3
    Yes thank you!!! Though rather than copying, it's probably better to just create a symlink sudo ln -s /Applications/Android\ Studio.app/Contents/jbr /Library/Java/JavaVirtualMachines/android-studio.jdk (this is the path for Android Studio Electric Eel. Check what's in your Android Studio app folder for the right path). Also, remove any other java installation you may already have in the JavaVirtualMachines folder. Feb 16 at 18:21
  • Yes that would be better! :-)
    – crjacinro
    Feb 17 at 7:19
  • 1
    This should be mark as the answer, thanks for detailed explanation. Not sure but in Windows I do not need to do all this stuff and didn't even need to specify JAVA_HOME. All I need is to put the JBR bin directory in %PATH% and everything will work such as keytool, jarsigner, etc. Aug 8 at 21:32
6

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/"
1
  • Just FYI: java bin located directly in the */bin folder of the provided path.
    – hamsternik
    Jun 21, 2022 at 15:49
5

From late 2022+, the answer for Mac User & Electric Eel+ has been changed to:

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

to .zshenv file in your home directory

1
  • 1
    That's the right export command, since in the latest Android Studio one of the folders name changed from jre to jbr Jun 19 at 14:26
4

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"
3
  • 2
    Works on M1 in 2022
    – Manish
    Oct 6, 2022 at 15:44
  • 2
    Saved me a lot of time. Thanks. Worked in Mac M1 Oct 27, 2022 at 12:50
  • 1
    Worked for me, I just updated my Studio to Eletric Eel Jan 16 at 12:10
3

The following worked for me (MacOs Ventura version 13.1)

  1. Enter the following command in the command line

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

  2. Create a new folder inside Applications/Android Studio.app/Contents and call it jre.

  3. Copy the contents of jbr folder into the newly created folderjre.

  4. Run flutter doctor.

Link : flutter issues link

0

I have three exports path in home/.zshrc mac system

  1. jdk path

  2. path Contains ordinary commands and shell scripts.

  3. Android sdk path

    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.0.5.jdk/Contents/Home

    export PATH="$PATH:/usr/local/bin"

    export ANDROID_HOME=$HOME/Library/Android/sdk

0

Solved!

  • Got to finder and right click it and press ## Show packages content ##
  • Got to Contents and create a new folder called jre
  • inside jre copy the content of the jbr folder
  • the flutter run
0

You can run the following command:

/usr/libexec/java_home

Use the output path as follows:

export JAVA_HOME="<path_you_received>"

Not the answer you're looking for? Browse other questions tagged or ask your own question.