DEV Community

Cover image for Change the default Java Version on macOS
Rithvik
Rithvik

Posted on

Change the default Java Version on macOS

Here, We are trying to change the version to Java SE 8

First run /usr/libexec/java_home -V which will output something like the following:

Matching Java Virtual Machines (3):
    16.0.2 (x86_64) "Oracle Corporation" - "Java SE 16.0.2" /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home
    1.8.202.08 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_202 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home
Enter fullscreen mode Exit fullscreen mode

Pick the version you want to be the default (1.8.0_202) then:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_202`
Enter fullscreen mode Exit fullscreen mode

Now when you run java -version you will see:

java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
Enter fullscreen mode Exit fullscreen mode

Add the

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_202`
Enter fullscreen mode Exit fullscreen mode

line to your shell’s init file.

Updating the .zshrc file should work:

nano ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

paste the line

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_202)
Enter fullscreen mode Exit fullscreen mode

at bottom of the file.

Press CTRL+X to exit the editor, Press Y to save your changes.

source ~/.zshrc
echo $JAVA_HOME
java -version
Enter fullscreen mode Exit fullscreen mode

Output should be:

java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
Enter fullscreen mode Exit fullscreen mode

Resources

Stackoverflow

Discussion (5)

Collapse
stealthmusic profile image
Jan Wedel

I just set up a new Mac for Java dev, and I can only recommend using SDKMan which allow to download, install and switch between different Java versions just with a CLI command:

sdkman.io/

Collapse
rithvik78 profile image
Rithvik Author

This is for executing RMI, where JDK 8 is recommended

Collapse
stealthmusic profile image
Jan Wedel

Ok… uhm. Did you mention RMI anywhere in your article where do you got the information that Java 8 is recommended for RMI over more recent versions? And anyways, I’m pretty sure that SDKMan also provides a JDK8. Did you try that?

Thread Thread
rithvik78 profile image
Rithvik Author

Why to try some other third party apps if native method is working fine..

Anyway thanks for advice I will definitely try it out :)

Thread Thread
stealthmusic profile image
Jan Wedel
  • sdkman is actually very widely used
  • it is very convenient and cross platform, so works on Mac, Windows
  • it will list all available jdks (Amazon, Oracle, MS etc) and versions
  • it downloads the jdks
  • it knows how to install them
  • it allows switching them instantly in the same shell

Honestly, I don’t see many reasons not to use it except for „it’s third party“