How to switch Java version on Mac?

You have two versions of JDK – let’s say Java 11 and Java 17 installed on your Mac. And you want to switch between default version – how could you do that?

It’s simple.

First of all, let’s figure out what Java versions we have:

# Find default version of Java - it's Java 17
$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home

# List all Java versions that we have
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
    17.0.2 (x86_64) "Oracle Corporation" - "Java SE 17.0.2" /Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home
    11.0.8 (x86_64) "Oracle Corporation" - "Java SE 11.0.8" /Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home

# Out of curiosity:
$ java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)

So, the default version is Java 17. Where is it set? It should be on ~/.bash_profile file:

$ cat ~/.bash_profile
export JAVA_HOME=$(/usr/libexec/java_home) # Here we go - use default Java version - Java 17 in my case

Let’s change default version to Java 11 by editing ~/.bash_profile file and specifying Java version that we want – Java 11 in my case:

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

And for the changes to take effect, either close & open the program or terminal. Or simply:

# Import changes in .bash_profile
$ source ~/.bash_profile 

# Let's check current Java version
$java -version
java version "11.0.8" 2020-07-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)

We use Java 11 now!

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *