Search code examples
macosterminaljava-home

How can I change the JAVA_HOME directory permanently on MAC?


I have a problem with my JAVA_HOME directory. Every time I want to use gradle I need to set my JAVA_HOME directory again and again.

I know how to export it to the right directory but after I close my terminal, I have to do it all over again.

What I do is

which java --> /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/bin/java

export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/"

to set the correct path. Everything is then set correctly, but when I close the terminal and get check the path again:

echo $JAVA_HOME -> /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Homeexport

Can you guys tell me how to change the directory permanently? I googled so often and found solutions which didn't help me at all


Solution

  • This isn't so much a Gradle question, as an environment question. You would need to set JAVA_HOME in your shell's configuration. If you can tell me what shell you're using, I can point you in the right direction.

    Since you're using ZSH, you can open ~/.zshenv and paste this in:

    export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/"
    

    Another solution would be to use something like Jenv to manage your Java environment, with the Gradle plugin enabled.