informationPC パソコン

Causes and Solutions for Android Studio “JAVA_HOME is not set”

When you open a project in Android Studio, run Gradle Sync or a build,
or run Gradle from Terminal,
an error called
“JAVA_HOME is not set”
may be displayed.

This error occurs when the environment variable
JAVA_HOME,
which indicates the location of Java,
is not set,
or when it does not point to the correct JDK location.

It may occur especially when a new JDK has been installed,
when the Java version has been changed,
when Gradle is run from Terminal,
or when the PC has been changed.

This article explains the main causes and solutions
when “JAVA_HOME is not set” is displayed in Android Studio.

What Is “JAVA_HOME is not set”?

“JAVA_HOME is not set” is an error displayed
when a program that uses Java cannot determine the installation location of the JDK.

JAVA_HOME
is an environment variable that indicates the location of the JDK to be used.

Gradle and other tools that use Java
may refer to
JAVA_HOME
to check the Java runtime environment.

JAVA_HOME is not set and no 'java' command could be found in your PATH.

If an error like this is displayed,
check the Java settings for
JAVA_HOME
or
PATH.

Point:
Even if it works normally from Android Studio,
“JAVA_HOME is not set” may be displayed only when Gradle is run from Terminal.
The Gradle JDK in Android Studio and the environment variables on the OS side
need to be checked separately.

Main Causes

The main causes of
“JAVA_HOME is not set”
include the following.

  • JAVA_HOME is not set
  • An incorrect path is set in JAVA_HOME
  • The location of the JRE rather than the JDK is specified
  • The JDK is not installed
  • An old path remains after the JDK has been removed
  • The path to the previous JDK is still set after Java has been updated
  • Multiple JDKs are installed
  • Java is not set in PATH
  • Terminal has not been restarted after changing environment variables
  • Android Studio and Terminal are using different Java environments
  • JAVA_HOME is not set in a CI or automated build environment

Check Whether Java Is Installed

First,
check whether Java is available for use.

Run the following command
in Terminal or Command Prompt.

java -version

If Java version information is displayed,
Java itself is being recognized.

If the command cannot be found,
the JDK may not be installed,
or
PATH
may not be configured correctly.

Check Whether the JDK Is Installed

Android app development uses the JDK,
rather than only running Java.

Even if Java exists on the PC,
the required JDK may not be installed.

Check whether a JDK corresponding to the Java version
required by the Android Gradle Plugin or Gradle
is installed.

Check the Current JAVA_HOME Setting

Check whether
JAVA_HOME
is set.

On macOS or Linux,
you can check it in Terminal as follows.

echo $JAVA_HOME

In Windows Command Prompt,
you can check it as follows.

echo %JAVA_HOME%

If nothing is displayed,
JAVA_HOME
may not be set.

Check the Path Set in JAVA_HOME

Even if a value is set in
JAVA_HOME,
the path is not necessarily correct.

If you remove a JDK
or change the Java version,
the path to an old JDK that no longer exists may remain.

Check whether the JDK actually exists
at the displayed path.

Check the JDK Location

In
JAVA_HOME,
specify the JDK root directory,
not the Java executable itself.

For example,
even if the
java
executable is located at:

/path/to/jdk/bin/java

you would normally specify
the location of the JDK itself in
JAVA_HOME
as follows.

/path/to/jdk

Check the JDK Location on macOS

On macOS,
you can use the following command
to check installed JDKs.

/usr/libexec/java_home -V

You can also use the following command
to check the location of the Java to be used.

/usr/libexec/java_home

The displayed JDK location
can be set in
JAVA_HOME.

Temporarily Set JAVA_HOME on macOS

On macOS or Linux,
you can temporarily set
JAVA_HOME
in Terminal.

export JAVA_HOME=$(/usr/libexec/java_home)

This setting is valid
for the current Terminal session.

Because the setting may be lost when Terminal is closed,
if you want to continue using it,
write it in the shell configuration file.

Permanently Set JAVA_HOME on macOS

If you use zsh on macOS,
you can add the
JAVA_HOME
setting to
~/.zshrc
or another configuration file.

export JAVA_HOME=$(/usr/libexec/java_home)

After making the setting,
restart Terminal
or reload the configuration file.

source ~/.zshrc

Then,
check whether the setting has been applied
with
echo $JAVA_HOME.

If You Want to Specify a Particular Java Version

If multiple JDKs are installed,
you can specify the Java version to use.

For example,
to use Java 17 on macOS,
you can specify it as follows.

export JAVA_HOME=$(/usr/libexec/java_home -v 17)

To use Java 21,
specify it as follows.

export JAVA_HOME=$(/usr/libexec/java_home -v 21)

Specify a Java version supported by
the Android Gradle Plugin and Gradle you are using.

Set JAVA_HOME on Windows

On Windows,
you can set
JAVA_HOME
from the system environment variables.

Create
JAVA_HOME
as an environment variable
and specify the folder of the installed JDK.

C:\Program Files\Java\jdk-17

The actual path differs
depending on the JDK that is installed.

After setting it,
close Command Prompt or Terminal once
and reopen it.

Also Check PATH

Depending on the error message,
it may indicate not only that
JAVA_HOME
is missing,
but also that Java does not exist in
PATH.

You may need to add the JDK’s
bin
directory to
PATH.

$JAVA_HOME/bin

On Windows,
it may be configured in the following form.

%JAVA_HOME%\bin

Check the Difference Between JAVA_HOME and PATH

JAVA_HOME
and
PATH
have different roles.

Setting Main Role
JAVA_HOME Indicates the location of the JDK to be used
PATH Allows commands such as java to be executed

Even if
java -version
works,
JAVA_HOME
may not be set.

Check the Gradle JDK in Android Studio

Android Studio allows you to configure
the JDK used by Gradle separately.

Gradle Sync may work normally inside Android Studio,
while
“JAVA_HOME is not set”
is displayed when
./gradlew
is run from Terminal.

This may occur when
the Gradle JDK is configured in Android Studio,
while
JAVA_HOME
on the OS side is not set.

Check the Java Used by Gradle

If the Gradle Wrapper can be executed,
you can check the Java used by Gradle
with the following command.

./gradlew --version

On Windows,
run it as follows.

gradlew.bat --version

Check the JVM information
and confirm whether the intended JDK is being used.

If Multiple JDKs Are Installed

If multiple JDKs such as Java 11,
Java 17,
and Java 21 are installed on the PC,
JAVA_HOME
may point to an unintended JDK.

Check the Gradle JDK in Android Studio,
JAVA_HOME,
and
java -version
individually.

Select a JDK supported by
the Android Gradle Plugin and Gradle.

If It Occurred After Updating the JDK

After updating the JDK,
if the previous JDK is removed,
JAVA_HOME
may continue to point to an old folder that no longer exists.

If
“JAVA_HOME is not set”
or another Java-related error occurs after updating the JDK,
change
JAVA_HOME
to the location of the current JDK.

If It Occurred After Updating Android Studio

When Android Studio is updated,
the JDK recommended for Gradle
or the requirements of the Android Gradle Plugin being used
may change.

Even if a newer JDK is being used inside Android Studio,
JAVA_HOME
on the OS side may still point to the previous JDK.

Check both
the Gradle JDK in Android Studio
and
JAVA_HOME.

If You Open an Old Project

An old Android project may assume
the JDK and Gradle versions that were used at the time.

Even if
JAVA_HOME
is set in the current environment,
that Java version may not be supported by the old Gradle.

Therefore,
check compatibility not only with the JDK,
but also with the Gradle Wrapper and Android Gradle Plugin.

Check Whether the JDK Version Is Appropriate

Setting
JAVA_HOME
does not mean that any JDK can be used.

Depending on the Android Gradle Plugin,
a certain minimum Java version may be required.

Conversely,
an older Gradle may not support a Java version that is too new.

Use a compatible combination of
JDK,
Gradle,
and Android Gradle Plugin.

If “Android Gradle plugin requires Java” Is Also Displayed

After setting
JAVA_HOME,
another error like the following may be displayed.

Android Gradle plugin requires Java 17 to run.
You are currently using Java 11.

In this case,
JAVA_HOME
itself is being recognized,
but the specified JDK version may be too old.

Change to a JDK corresponding to
the Java version required by the Android Gradle Plugin.

If “Unsupported class file major version” Is Also Displayed

After changing
JAVA_HOME,
“Unsupported class file major version”
may be displayed.

In this case,
Gradle may be too old
for the newly specified JDK.

Check the compatibility between
the Gradle Wrapper version
and the JDK being used.

Restart Terminal After Changing Environment Variables

Even if you change
JAVA_HOME
or
PATH,
the new settings may not be applied
to a Terminal window that is already open.

After changing the settings,
close Terminal or Command Prompt
and reopen it.

Then,
check the settings again
with the following commands.

java -version

echo $JAVA_HOME

Restart Android Studio

After changing the JDK or environment variable settings,
restarting Android Studio
may apply the new settings.

After restarting,
run Gradle Sync or the build again.

Run Gradle Sync Again

After correcting the Java or Gradle JDK settings,
run Gradle Sync again.

Run
Sync Project with Gradle Files
or a similar option from Android Studio.

If the Java environment is recognized correctly,
Gradle Sync may proceed normally.

If the Error Occurs Only on the Command Line

Android Studio may be able to build normally,
while
“JAVA_HOME is not set”
is displayed only when
./gradlew
is run from Terminal.

In this case,
check
JAVA_HOME
and
PATH
on the Terminal side,
rather than the Gradle JDK in Android Studio.

Android Studio and Terminal
may refer to Java in different ways.

If It Occurs in a CI or Automated Build Environment

Even if the build works normally in the local environment,
“JAVA_HOME is not set”
may occur in a CI or automated build environment.

In this case,
check whether a JDK is available in the CI environment
and whether
JAVA_HOME
is configured correctly.

If different Java settings are used
in the local environment and CI environment,
the results may differ even for the same project.

What to Check for Each Error

Depending on the Java- or Gradle-related error,
you can narrow down what should be checked to some extent.

Displayed Message Main Points to Check
JAVA_HOME is not set JAVA_HOME, JDK, PATH
Android Gradle plugin requires Java Gradle JDK, JDK, Android Gradle Plugin
Unsupported class file major version JDK, Gradle, Android Gradle Plugin
Minimum supported Gradle version is … Gradle Wrapper, Android Gradle Plugin
Gradle sync failed Detailed error, Java, Gradle, and plugin settings

Steps to Check If the Problem Is Not Resolved

If you do not know the cause,
checking the following in order makes it easier to narrow down the problem.

  1. Check the full “JAVA_HOME is not set” error message
  2. Check whether Java can be used with java -version
  3. Check whether the required JDK is installed
  4. Check whether JAVA_HOME is set
  5. Check whether JAVA_HOME points to the correct JDK location
  6. Check whether a folder such as bin is being specified directly instead of the JDK
  7. Check the Java setting in PATH
  8. Check whether multiple JDKs are installed
  9. Check the Gradle JDK in Android Studio
  10. Check whether the JDK being used is supported by Gradle and the Android Gradle Plugin
  11. Restart Terminal after changing environment variables
  12. Restart Android Studio if necessary
  13. Run Gradle Sync again
  14. If it occurs only on the command line, check the settings on the Terminal side
  15. For a CI environment, check the JDK and JAVA_HOME on the CI side

Important:
If “JAVA_HOME is not set” is displayed,
it is important not only to install Java,
but also to check whether
JAVA_HOME
points to the root directory of a JDK that actually exists,
and whether that JDK is supported by Gradle and the Android Gradle Plugin.

Summary

“JAVA_HOME is not set”,
which is displayed in Android Studio or Gradle,
is an error that occurs when
JAVA_HOME,
which indicates the location of the JDK,
is not set
or does not point to the correct JDK.

It may occur especially when a new JDK has been installed,
when the Java version has been changed,
or when Gradle is run from Terminal.

First,
check
java -version
and the current
JAVA_HOME
setting,
and confirm whether the location of the JDK to be used
is specified correctly.

Then,
also check the Gradle JDK in Android Studio,
PATH,
and compatibility with Gradle and the Android Gradle Plugin,
and after changing the settings,
restart Terminal and Android Studio
and run Gradle Sync again
to efficiently narrow down the cause.