How to get MacOS (Catalina) to use custom Java version?

Viewed 843

I am trying to change the default Java version on my machine from version 15 (i.e. openjdk 15.0.1) to version 11 (i.e. openjdk version 11.0.2). I have followed various solutions provided online, but at the end of the day, I just want the following result:

$ java --version
openjdk 11.0.9 2020-10-20
OpenJDK Runtime Environment (build 11.0.9+9)
OpenJDK 64-Bit Server VM (build 11.0.9+9, mixed mode, sharing)

But instead, I get:

$ java --version
openjdk 15.0.1 2020-10-20
OpenJDK Runtime Environment (build 15.0.1+9)
OpenJDK 64-Bit Server VM (build 15.0.1+9, mixed mode, sharing)

Most solutions that I've found, suggest setting a new value to my JAVA HOME environmental variable (both in-line and in .bash_profile), where as of now, I have as:

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

Even removing jdk-15.0.1.jdk entirely from /Library/Java/JavaVirtualMachines/ didn't seem to change anything. So, this JDK must be defined and used somewhere else, and its existance is overriding anything I put in my .bash_profile file. Has anyone else experienced this before? Is this change possible?

2 Answers

It turns out that the issue was within my .bash_profile. While I'm not sure if this is the case for everyone, my PATH environment variable settings superseded my JAVA_HOME environment variable, and my PATH variable had reference my JAVA_HOME variable inside it. This was resolved by changing the following in my .bash_profile file.

If JAVA_HOME is the only variable added, then the following should work.

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

export PATH=$JAVA_HOME/bin

If there are others then this should work.

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

export SOME_OTHER_PATH=/somepath

export PATH=$SOME_OTHER_PATH:$JAVA_HOME/bin

Thanks everyone for their help!

Make sure that the first line in your .bash_profile file that talks about JAVA_HOME is this:

export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.9)

You can run /usr/libexec/java_home -V to see a list of all your installed Java Virtual Machines. On my machine, I have the following (mine will look slightly different because this output is from Big Sur):

Matching Java Virtual Machines (6):
    15.0.1 (x86_64) "UNDEFINED" - "OpenJDK 15.0.1" /usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home
    11.0.9 (x86_64) "UNDEFINED" - "OpenJDK 11.0.9" /usr/local/Cellar/openjdk@11/11.0.9/libexec/openjdk.jdk/Contents/Home
    11.0.6 (x86_64) "Oracle Corporation" - "Java SE 11.0.6" /Library/Java/JavaVirtualMachines/jdk-11.0.6.jdk/Contents/Home
    11.0.1 (x86_64) "Oracle Corporation" - "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
    1.8.0_275 (x86_64) "UNDEFINED" - "OpenJDK 8" /usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home
    1.8.0_131 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
/usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home

The line at the very bottom (which may not exist in Catalina, but which you can get by running /usr/libexec/java_home is your currently selected JVM.

Related