Flutter's Android build stops: JAVA_HOME is not set
ERROR: 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.
Fix it fast
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 -version2Match what you see, then jump to its fix
- 1
ERROR: 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
ERROR: JAVA_HOME is set to an invalid directoryJAVA_HOME is set, but to a folder that is not a JDKFix - 3
Running from Android Studio succeeds, while the same project fails in a terminal with the JAVA_HOME errorStudio builds work, the terminal failsFix
- 1
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 directoryPlease 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 --debugConfirmed by developer.android.com
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/jdk3Verify
The ls command above lists bin/java and java -version prints a version.
ls "$JAVA_HOME/bin/java" && java -versionConfirmed by raw.githubusercontent.com
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_HOME2Fix
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 JDK3Verify
The command-line build and the Studio build use the same Java.
java -versionConfirmed by developer.android.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Mobile apps
flutter doctor cannot find the Java that comes with Android Studio
Unable to find bundled Java version.
Flutter looks for the Java inside Android Studio first, then JAVA_HOME, then any java on PATH.
3 causes, each with a fixMobile apps
flutter run hangs for minutes on Running Gradle task 'assembleDebug'
Downloading https://services.gradle.org/distributions/gradle-8.14-all.zip
That line only says Gradle started; the wait is inside Gradle.
3 causes, each with a fixMobile apps
Flutter Android build fails with Unsupported class file major version
Unsupported class file major version 65
The number is the Java version that tried to read a file: 61 is Java 17, 65 is Java 21.
3 causes, each with a fixMobile apps
Flutter release build cannot open the keystore: HmacPBESHA256 not available
Algorithm HmacPBESHA256 not available
The keystore is a PKCS12 file protected by a newer algorithm than the Java that is reading it understands.
3 causes, each with a fixMobile 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 fixMobile apps
Android Studio or its Gradle build runs out of Java heap memory
Android Studio shows a notification about recommended memory settings
Two separate Java processes can run out of heap: the IDE itself and the Gradle daemon that builds your app.
3 causes, each with a fix