Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 618
soshial Avatar asked Apr 04 '17 15:04

soshial


People also ask

What JDK is bundled with Android Studio?

Set the JDK versionA copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects.

Should JAVA_HOME point to JDK?

If you're doing any sort of development, or building with Maven or Ant, you need to point to the JDK (Java Development Kit) where utilities such as javac (the Java Compiler) reside. Otherwise, you can point to the JRE (Java Runtime Environment). The JDK contains everything the JRE has and more.


1 Answers

Add the correct string to .bash_profile (and reload with source .bash_profile):

  1. MacOS versions before Big Sur: export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home/.
  2. Catalina, Big Sur, Mentere and above: export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home

If you're using Catalina and above, you most probably use zsh as a shell instead of bash. In that case, add it to .zshrc or .zprofile instead of .bash_profile in your home directory.

Don't forget to restart the operating system after.

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.

like image 138
soshial Avatar answered Sep 22 '22 08:09

soshial