skip to Main Content

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


  1. 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;

    Login or Signup to reply.
    1. Open .zshrc in a text editor:

      nano ~/.zshrc
      
    2. 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:

      export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
      
    3. Alternative Method: If the above path doesn’t work, you can use macOS’s java_home utility to set JAVA_HOME automatically:

      export JAVA_HOME=$(/usr/libexec/java_home)
      
    4. Save the changes and reload .zshrc to apply:

      source ~/.zshrc
      
    5. After completing these steps, you can check that JAVA_HOME is set correctly by running:

      echo $JAVA_HOME
      

    This should display the path to your Java installation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search