I have an Flutter app in android studio on my Mac and I accidentally reset my JAVA_HOME path in my project trying to update java for my project.
This gives me the error:
JAVA_HOME is set to an invalid directory: Users/{My UserName}/Library/Java/JavaVirtualMachines/openjdk-23.0.1
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
To find my java version and location I ran the following command in my terminal:
/usr/libexec/java_home -V
returned:
Matching Java Virtual Machines (1):
1.7.45.18 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
I then opened my .zshrc file and added the following lines:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7.45.18)
export PATH=$JAVA_HOME/bin:$PATH
To confirm the path I ran:
echo $JAVA_HOME
returned:
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
When trying to build my app again I get the same error I got before. So I tried running the following command inside of my app directory terminal:
export JAVA_HOME=“/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home”
returned:
export: not valid in this context: /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
I don't understand what this error means.
How do I set my project JAVA_HOME to my computers JAVA_HOME?
EDIT: g00se and John Bollinger have pointed out that my error is probably in Android studio. So I downloaded a new JDK in Android studio (File > Project Structure...)
I reset my JAVA_HOME path in my .zshrc file to match my new jdk file path.
running flutter run
still gives me the error :
ERROR: JAVA_HOME is set to an invalid directory: Users/{MyName}/Library/Java/JavaVirtualMachines/openjdk-23.0.1
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
fluttor doctor -v
gives me the error:
Java binary at: Users/{MyName}/Library/Java/JavaVirtualMachines/openjdk-23.0.1/bin/java
✗ Cannot execute Users/{MyName}/Library/Java/JavaVirtualMachines/openjdk-23.0.1/bin/java to determine the version
Update:
flutter analyze --suggestions
returns
[✗] Java/Gradle/Android Gradle Plugin:
Incompatible Java/Gradle versions.
Java Version: null, Gradle Version: 7.6.3
So even though I am getting the same JAVA_HOME paths from echo $JAVA_HOME
and what I see in my project structure inside of android studio I'm getting a null Java version here...
After making sure my JAVA_HOME paths were compatible my final issue was caused by incompatible Java and Gradle versions.
See Compatibility Matrix. I had installed jdk 23, which was incompatible with gradle 7.6.
In Android Studio: File > Project Structure > Add New SDK > Download JDK...
Select the compatible version for you, in my case 19.