Developer guide · Windows and Mac
Fix "JAVA_HOME is set to an invalid directory" on Mac and Windows
By Aqib Javed · Updated Oct 22, 2026
The error means JAVA_HOME names a folder that has no JDK in it, usually one that was removed or updated. On a Mac, set export JAVA_HOME=$(/usr/libexec/java_home -v 17) in ~/.zprofile. On Windows, set the JAVA_HOME user variable to the JDK's root folder, such as C:\Program Files\Java\jdk-17, not its bin folder. Then open a new terminal, because programs only read environment variables when they start.
Check what JAVA_HOME points to
If the java file isn't there, JAVA_HOME is pointing at a folder that no longer holds a JDK.
# Mac
echo $JAVA_HOME
ls "$JAVA_HOME/bin/java"
# Windows (Command Prompt)
echo %JAVA_HOME%
dir "%JAVA_HOME%\bin\java.exe"Find the JDKs you have
- Mac: /usr/libexec/java_home -V lists every installed JDK and its path. They usually live in /Library/Java/JavaVirtualMachines.
- Windows: look in C:\Program Files\Java, C:\Program Files\Eclipse Adoptium and similar folders.
- Both: Android Studio ships its own JDK in its jbr folder, which you can point JAVA_HOME at too.
Fix it on a Mac
Add one line to ~/.zprofile (or ~/.zshrc), with the version you need, then run source ~/.zprofile or open a new terminal:
export JAVA_HOME=$(/usr/libexec/java_home -v 17)Fix it on Windows
- Open the Start menu, search for "environment variables" and choose Edit environment variables for your account.
- Select JAVA_HOME and click Edit, or click New if it doesn't exist.
- Set it to the JDK's root folder, for example C:\Program Files\Java\jdk-17, without \bin at the end. Click OK.
- Close and reopen your terminal and IDE.
# the same in PowerShell, for your user account
[Environment]::SetEnvironmentVariable('JAVA_HOME', 'C:\Program Files\Java\jdk-17', 'User')Pick the Java version your project needs
For Android builds, Android Gradle Plugin 8 and later need Java 17 or newer, and every Gradle version has a newest Java it supports (Gradle's compatibility matrix lists them). Android Studio can also use its own JDK for Gradle, set under Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK, which is why a build can work in Studio but fail in the terminal.
Doing it with Stackyard
On Windows, Stackyard's doctor spots a JAVA_HOME that points to a missing folder and offers to point it at an installed JDK in one click. On both Windows and Mac it lists every JDK it finds, installs a new one from its marketplace and sets JAVA_HOME for you, and warns when the Java a Flutter app builds with doesn't fit that project's Gradle and Android Gradle Plugin. It's free and open source.
Questions
Should JAVA_HOME include \bin?
- No. Point it at the JDK's root folder. Tools add bin themselves.
Why does the build work in Android Studio but not in the terminal?
- Android Studio can run Gradle with its own JDK (the Gradle JDK setting), while the terminal uses JAVA_HOME. Make them match.
User variable or system variable on Windows?
- A user variable needs no admin rights and wins over the system one for your account, so it's the simplest fix.
Do I need to restart my computer?
- No. Open a new terminal and restart your IDE: they read environment variables only when they start.