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?
2
Answers
you should go to your system>>enviorment variables>>click on the edit enviorment variables and then edit the existing path variables.
If you want you could even watch the tutorial about this in detail;
Open
.zshrc
in a text editor:Add the path to your Java installation at the end of the file. Replace the path below if your Java version or installation location is different:
Alternative Method: If the above path doesn’t work, you can use macOS’s
java_home
utility to setJAVA_HOME
automatically:Save the changes and reload
.zshrc
to apply:After completing these steps, you can check that
JAVA_HOME
is set correctly by running:This should display the path to your Java installation.