I am developing both android native and flutter projects using android studio. My android native project jdk sets to /Applications/AndroidStudio.app/Contents/jbr/Contents/Home
and I have downloaded jdk17.0.12 and installed it in my machine. I also installed jdk23 and removed that as I want to continue with jdk17. I have configured JAVA_HOME by using nano ~/.zshrc
and added the path. Here is my entire list.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
export ANDROID_HOME=/Users/$USER/Library/Android/sdk
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
export PATH=/opt/homebrew/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr$
When I try to run my flutter app I'm getting the below error.
ERROR: JAVA_HOME is set to an invalid directory: /Users/srvenk/Library/Java/JavaVirtualMachines/openjdk-23/Contents/Home
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
Error: Gradle task assembleDebug failed with exit code 1
I tried running flutter doctor
and I got one error which is related to JAVA_HOME.
Cannot execute /Users/srvenk/Library/Java/JavaVirtualMachines/openjdk-23/Contents/Home/bin/java to determine the version
I have removed jdk23 from my machine and set the JAVA_HOME variable to /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
. But still I am getting this same issue. I have tried to invalidate the cache in android studio and restarted my machine multiple time. But Still no luck. Could someone help me to solve this problem?
Hey What happens if you do echo $JAVA_HOME
in your shell,
And in android studio you could go to Preferences > Build, Execution, Deployment > Build Tools > Gradle, There should be a Gradle JDK option try Changing it to JDK 17.
Also, try these steps:
Install the Correct JDK Version For Flutter's compatibility, it's often required to have a compatible JDK version. You can use Homebrew to install the desired JDK. As of the latest updates and compatibility issues, you might want to consider JDK 21.
brew install openjdk@21
Update Your Profile Configuration Files Update your shell configuration files such as .bash_profile or .zshenv to point to the newly installed JDK. Open these files in a text editor:
export PATH="$PATH:/Users/zhelon/development/flutter/bin:$JAVA_HOME/bin"
export JAVA_HOME="/opt/homebrew/opt/openjdk@21"
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
Apply the Configuration Changes For the changes to take effect, you need to reload these files:
source ~/.bash_profile
source ~/.zshenv
Configuring Flutter to Use the Correct JDK Use Flutter’s command-line tool to set the JDK directory explicitly:
flutter config --jdk-dir $JAVA_HOME
Run Your Flutter Project Again Now, try running your Flutter project to verify if the issue is resolved:
flutter run
Troubleshooting Tips
If you still encounter issues, ensure:
Source: https://www.devgem.io/posts/resolving-flutter-s-java-home-error-on-macos-step-by-step-guide