soshial
soshial

Reputation: 6788

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

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?

Upvotes: 146

Views: 116321

Answers (15)

sultanmyrza
sultanmyrza

Reputation: 5412

My latest .zshrc on my Mac M1 (android studio ladybug)

# Set the ANDROID_HOME environment variable
export ANDROID_HOME=~/Library/Android/sdk

# Add Android SDK tools to the PATH
# This allows you to run adb, emulator, etc., from the command line
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

# Set the JAVA_HOME environment variable
export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home

# Add the JDK bin directory to the PATH
# This allows you to run Java commands from the command line
export PATH=$PATH:$JAVA_HOME/bin

Upvotes: 1

Malak
Malak

Reputation: 451

Updated July 2024

Macos Version: Sonoma 14.5

Android Studio Code Version: Android Studio Koala 2024.1.1

Find out which shell you are using, bash or zsh, for me its zsh so i am updating .zshenv file by changing this line:

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

To this line :

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

For zsh shell you need to update -> ~/.zshenv or ~/.zshrc file

For bash shell you need to update -> ~/.bash_profile or ~/.bashrc file

If don't know what shell you are using simply write in terminal echo $SHELL

Upvotes: 4

Kishan Vaghela
Kishan Vaghela

Reputation: 7938

For Android Studio Koala use,

export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home
  1. touch .zshenv in home dir (if .zshenv is not available)
  2. open .zshenv
  3. add export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home
  4. source .zshenv
  5. verify with java -version

Upvotes: 3

soshial
soshial

Reputation: 6788

Solution:

  1. (Updated 2024) 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.

Upvotes: 291

Cleopas Mwape
Cleopas Mwape

Reputation: 507

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.

Upvotes: 8

Erick Gutierrez
Erick Gutierrez

Reputation: 516

You can run the following command:

/usr/libexec/java_home

Use the output path as follows:

export JAVA_HOME="<path_you_received>"

Upvotes: 2

solamour
solamour

Reputation: 3214

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.

Upvotes: 112

r.saiprasad
r.saiprasad

Reputation: 53

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

Upvotes: 4

Tabatha Moreno
Tabatha Moreno

Reputation: 89

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

Upvotes: 1

Beatle Refractor
Beatle Refractor

Reputation: 659

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

Upvotes: 0

T D Nguyen
T D Nguyen

Reputation: 7603

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

Upvotes: 5

Thiago
Thiago

Reputation: 13302

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"

Upvotes: 4

Yvgen
Yvgen

Reputation: 2212

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

Upvotes: 6

crjacinro
crjacinro

Reputation: 177

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.

Upvotes: 13

busylee
busylee

Reputation: 2560

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.

Upvotes: 14

Related Questions