126

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?

soshial
  • 5,906
  • 6
  • 32
  • 40

10 Answers10

245

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.

soshial
  • 5,906
  • 6
  • 32
  • 40
  • 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 '17 at 12:55
  • Also, after this Android Studio stopped freezing on startup – soshial Dec 08 '17 at 10:11
  • 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! – peresisUser May 14 '19 at 14:30
  • 2
    Though java -version works fine, /usr/libexec/java_home still fails for me after adding JAVA_HOME – Dimezis Dec 03 '19 at 17:17
  • 3
    Sweet. I didn't have to restart my mac. – Aba Dec 04 '19 at 10:10
  • This is perfect. The Android Studio Installer should have a choice to do this on installation. Thank you so much! – Lucas P. Mar 03 '20 at 19:03
  • Yeah, Literally for some magical reason, restarting the OS fixes this. – Shivam Pokhriyal Jul 22 '20 at 05:48
  • How to find the one n Win 10? – Hasan A Yousef Oct 14 '20 at 09:10
  • 2
    if you're using Catalina and above, you probably use a zsh shell by default. So instead of adding it to the bash profile, create a .zshrc file in the home directory. Then in Terminal type `source ~/.zshrc` – Raphael Pinel Nov 30 '20 at 09:02
  • @RaphaelPinel, thanks for the advice about Catalina and Big Sur. You may freely edit my answer. Or should I do it? – soshial Dec 01 '20 at 08:32
  • 7
    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/"` – Sei Flavius Apr 13 '21 at 18:38
  • I'm running Monterey and just installed Android Studio 2020.3.1. There was no `jdk` directory within `jre`. So, this is what worked for me: `export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home` – Felix Livni Nov 14 '21 at 01:26
  • setting it on .zshrc will be better. as it is the default source. – Vikas Pandey Dec 19 '22 at 11:13
  • 2
    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` – Sohayb Hassoun Jan 19 '23 at 14:18
  • for me with zsh and electric eel: ~/.zshrc -> export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home/" – JacksOnF1re Feb 23 '23 at 11:48
94

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.

solamour
  • 2,764
  • 22
  • 22
  • Thank you solamour, I also just fresh install Android Studio Arctic Fox on Big Sur. And it work – Eric Wijaya Aug 17 '21 at 03:22
  • 2
    "/Applications/Android\ Studio.app/Contents/jre/Contents/Home/" it's not working. – Nelu B Sep 06 '21 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 07 '21 at 00: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 https://fluttercorner.com/flutter-unable-to-find-bundled-java-version-after-updated-android-studio-arctic-fox2020-3-1-on-m1-apple-silicon/ – Nelu B Sep 07 '21 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 09 '21 at 04:06
  • this is the answer I am looking for, thanks – Mesut GUNES Nov 25 '21 at 14:27
  • Thanks, this is what tripped me up eventually. I was trying to setup Unreal Engine 5 on macos M1 using SetupAndroid.command and running zsh instead of bash. I ended up replacing all references to .bash_profile to .zprofile as well as hardcoding the full path to the /Users//Library/Android/sdk/cmdline-tools/latest/bin/sdkmanager to get it to run. – thiezn May 29 '22 at 18:34
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.

busylee
  • 2,540
  • 1
  • 16
  • 35
9

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.

crjacinro
  • 135
  • 1
  • 6
  • 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. – walkingbrad Feb 16 '23 at 18:21
  • Yes that would be better! :-) – crjacinro Feb 17 '23 at 07:19
  • 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. – Bitwise DEVS Aug 08 '23 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/"
Yvgen
  • 2,036
  • 2
  • 23
  • 27
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

T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
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"
Thiago
  • 12,778
  • 14
  • 93
  • 110
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

Shalabyer
  • 555
  • 7
  • 23
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