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

12

137

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?

Conscript answered 4/4, 2017 at 15:18 Comment(0)
C
273

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.

Conscript answered 5/4, 2017 at 16:47 Comment(15)
how to get keytool when using the OpenJDK? It says "To use the keytool command-line tool you need to install a JDK"Bullyrag
Also, after this Android Studio stopped freezing on startupConscript
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!Kant
Though java -version works fine, /usr/libexec/java_home still fails for me after adding JAVA_HOMEHillie
Sweet. I didn't have to restart my mac.Constitute
This is perfect. The Android Studio Installer should have a choice to do this on installation. Thank you so much!Odilo
Yeah, Literally for some magical reason, restarting the OS fixes this.Kellie
How to find the one n Win 10?Dished
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 ~/.zshrcExpurgatory
@RaphaelPinel, thanks for the advice about Catalina and Big Sur. You may freely edit my answer. Or should I do it?Conscript
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/"Pukka
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/HomeSupremacy
setting it on .zshrc will be better. as it is the default source.Zephan
With the latest "Electric Eel" release, /jre/ changed to /jbr/so it's now export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jbr/Contents/HomeReceiver
for me with zsh and electric eel: ~/.zshrc -> export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home/"Austenite
U
110

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.

Usquebaugh answered 12/8, 2021 at 4:41 Comment(9)
Thank you solamour, I also just fresh install Android Studio Arctic Fox on Big Sur. And it workAnthroposophy
"/Applications/Android\ Studio.app/Contents/jre/Contents/Home/" it's not working.Homophonous
@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.Usquebaugh
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/…Homophonous
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 flagBordelon
this is the answer I am looking for, thanksHeliograph
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/<my user>/Library/Android/sdk/cmdline-tools/latest/bin/sdkmanager to get it to run.Pellegrini
Before I used the above path inside "" in .zshrc file, after removing the quotes it started working. Old: export JAVA_HOME="/Applications/Android\ Studio.app/Contents/jbr/Contents/Home" New: export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jbr/Contents/HomeBulley
I think you should have "no quotes with backslash" OR "quotes without backslash". It most likely won't work if you have "quotes with backslash", because the backslash won't be recognized as the escape.Usquebaugh
S
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.

Sherl answered 9/9, 2021 at 18:35 Comment(1)
Thank you! It worked! I just need to close terminals and open new terminals to check.Yolandayolande
S
12

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.

Sindee answered 18/9, 2021 at 14:18 Comment(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.Levin
Yes that would be better! :-)Sindee
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.Pastypat
M
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/"
Mireielle answered 8/5, 2022 at 13:19 Comment(1)
Just FYI: java bin located directly in the */bin folder of the provided path.Bestrew
G
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

Generalissimo answered 19/12, 2022 at 21:7 Comment(1)
That's the right export command, since in the latest Android Studio one of the folders name changed from jre to jbrAdenoidectomy
V
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"
Volplane answered 25/7, 2022 at 11:39 Comment(3)
Works on M1 in 2022Potts
Saved me a lot of time. Thanks. Worked in Mac M1Underlay
Worked for me, I just updated my Studio to Eletric EelSlipstream
E
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

Eke answered 20/1, 2023 at 8:18 Comment(0)
S
2

UPDATED 2024

I'm using MacOs Sonoma and Android Studio Hedgehog. Here is how I solved it:

  1. If you do not already have .zshrc file in your home then add it and if you already do have then open it using any text editor. ( Hint: .zshrc is hidden by default, so you might need to show hidden files to see it by pressing command + Shift + . )

  2. Add export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home/" to it and save it.

  3. Close and re-open your terminal or run source ~/.zshrc in your terminal for changes to take effect.

  4. Finally you can now run Java -version to confirm issue has been fixed.

Sickroom answered 13/2 at 6:20 Comment(1)
Helped to solve issue with command line buildsEskridge
T
1

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
Tatman answered 21/1, 2023 at 22:35 Comment(0)
K
1

You can run the following command:

/usr/libexec/java_home

Use the output path as follows:

export JAVA_HOME="<path_you_received>"
Karachi answered 2/11, 2023 at 18:24 Comment(0)
R
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

Rumpf answered 19/1, 2023 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.