JAVA_HOME on MacOS

This document shows how to install, change, and pick the java version on MacOS

/usr/libexec/java_home is the simplest maintainable way of setting JAVA_HOME on macOS.

You can use java_home to:

  • Find all installed JDKs

  • Get value for JAVA_HOME for a specific JDK version

  • Get the value of JAVA_HOME for the default JDK version

  • Execute specific versions of Java commands

Check Installed Java Versions

/usr/libexec/java_home -V

//you will see something like:

Matching Java Virtual Machines (7):
    14.0.1 (x86_64) "Oracle Corporation" - "Java SE 14.0.1" /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home
    11.0.12 (x86_64) "Oracle Corporation" - "Java SE 11.0.12" /Library/Java/JavaVirtualMachines/jdk-11.0.12.jdk/Contents/Home
    11.0.2 (x86_64) "Oracle Corporation" - "OpenJDK 11.0.2" /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
    1.8.291.10 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_312 (arm64) "Azul Systems, Inc." - "Zulu 8.58.0.13" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
    1.8.0_281 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
    1.8.0_261 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home

Check value of current used Java Version

/usr/libexec/java_home

Set JAVA_HOME to specific Java Version

export JAVA_HOME='/usr/libexec/java_home -v 1.7'

//if you want to set it to the default java version

export JAVA_HOME='/usr/libexec/java_home'

Remove Java SDK

sudo rm -rf /Library/Java/JavaVirtualMachines/jdk-<version>.jdk

Last updated