Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Flutter's Android build stops: JAVA_HOME is not set

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

Short answer

The Gradle wrapper script needs a Java runtime and looks in two places: the JAVA_HOME variable, then a java command on PATH. This error means both were empty. Install a JDK, or reuse the one inside Android Studio, and point JAVA_HOME at its folder. A second message says JAVA_HOME is set to an invalid directory.

  • Applies to Flutter 3.x stable, Gradle wrapper, JDK 17 or newer, macOS 26, Ubuntu 24.04, Windows 11
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the terminal what it sees. If echo prints nothing and java is not found, the first cause applies. If echo prints a path, the second one does.

    echo $JAVA_HOME
    java -version
  2. 2Match what you see, then jump to its fix

    1. 1ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.No JDK is reachable: JAVA_HOME is empty and java is not on PATHFix
    2. 2ERROR: JAVA_HOME is set to an invalid directoryJAVA_HOME is set, but to a folder that is not a JDKFix
    3. 3Running from Android Studio succeeds, while the same project fails in a terminal with the JAVA_HOME errorStudio builds work, the terminal failsFix

What does the error look like?

  • ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
  • ERROR: JAVA_HOME is set to an invalid directory
  • Please set the JAVA_HOME variable in your environment to match the location of your Java installation.

Cause 1 of 3

No JDK is reachable: JAVA_HOME is empty and java is not on PATH

1Check

When Gradle runs in a terminal, the JAVA_HOME variable decides which JDK is used. If it is not set, the java command on PATH is used. Android Studio ships its own JDK in a jbr folder, but a terminal does not use it on its own.

which java
echo "JAVA_HOME=$JAVA_HOME"

2Fix

Install a JDK of version 17 or newer (the Android Gradle plugin 8.x needs 17), then export JAVA_HOME as the folder that contains bin/java. The jbr folder inside the Android Studio installation is a valid choice.

# example for a JDK installed at /opt/jdk-17
export JAVA_HOME=/opt/jdk-17
export PATH="$JAVA_HOME/bin:$PATH"

3Verify

java answers, and the Android build gets past the wrapper.

java -version
flutter build apk --debug

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 3

JAVA_HOME is set, but to a folder that is not a JDK

1Check

The wrapper script prints this when the variable holds a path that does not exist or has no bin/java, for example after a JDK was uninstalled or upgraded to a new folder name.

echo $JAVA_HOME
ls "$JAVA_HOME/bin/java"

2Fix

Point the variable at the home folder of a JDK that exists now, or remove the stale line from your shell profile or system variables so the java on PATH is used.

export JAVA_HOME=/path/to/existing/jdk

3Verify

The ls command above lists bin/java and java -version prints a version.

ls "$JAVA_HOME/bin/java" && java -version

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

Studio builds work, the terminal fails

1Check

Studio runs Gradle with the JDK chosen in its own settings, while a terminal uses JAVA_HOME or the java on PATH. They can differ.

echo $JAVA_HOME

2Fix

Set JAVA_HOME to the same JDK that the Gradle JDK setting in Android Studio uses (Settings, Build Tools, Gradle), so both agree.

# Android Studio: Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK

3Verify

The command-line build and the Studio build use the same Java.

java -version

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Set JAVA_HOME once in your shell profile or system variables and use the same JDK in Android Studio.
  • Keep the variable pointing at the JDK folder, not at its bin folder.
  • After installing or removing a JDK, run java -version and echo the variable again.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

A phone is plugged in over USB but Android Studio and adb do not list it

adb devices prints List of devices attached with nothing below it

adb lists a phone only when four things hold: USB debugging is on, you accepted the computer's RSA key on the phone, the computer has a driver or udev rule for the device, and the cable carries data to a running adb server.

6 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.