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

Matching Java Virtual Machines (3):
1.8.0_05, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
1.6.0_65-b14-462, x86_64:   "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

Pick the version you want to be the default (1.6.0_65-b14-462 for arguments sake) then:

export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`

or you can specify just the major version, like:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Now when you run java -version you will see:

java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

Add the export JAVA_HOME… line to your shell’s init file.

For Bash (as stated by antonyh):

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

For Fish (as stated by ormurin)

set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)

Updating the .zshrc file should work:

nano ~/.zshrc

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

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

source ~/.zshrc
echo $JAVA_HOME
java -version
Answer from markhellewell on Stack Overflow
Top answer
1 of 16
2562

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

Matching Java Virtual Machines (3):
1.8.0_05, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
1.6.0_65-b14-462, x86_64:   "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

Pick the version you want to be the default (1.6.0_65-b14-462 for arguments sake) then:

export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`

or you can specify just the major version, like:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Now when you run java -version you will see:

java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

Add the export JAVA_HOME… line to your shell’s init file.

For Bash (as stated by antonyh):

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

For Fish (as stated by ormurin)

set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)

Updating the .zshrc file should work:

nano ~/.zshrc

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

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

source ~/.zshrc
echo $JAVA_HOME
java -version
2 of 16
608

This answer is an attempt to address: how to control java version system-wide (not just in currently running shell) when several versions of JDK are installed for development purposes on macOS El Capitan or newer (Sierra, High Sierra, Mojave). As far as I can tell, none of the current answers do that (*).

As a developer, I use several JDKs, and I want to switch from one to the other easily. Usually I have the latest stable one for general use, and others for tests. But I don't want the system (e.g. when I start my IDE) to use the latest "early access" version I have for now. I want to control system's default, and that should be latest stable.

The following approach works with Java 7 to 12 at least (early access at the time of this writing), with Oracle JDK or OpenJDK (including builds by AdoptOpenJDK produced after mid-October 2018).

Solution without 3rd party tools:

  • leave all JDKs at their default location, under /Library/Java/JavaVirtualMachines. The system will pick the highest version by default.
  • To exclude a JDK from being picked by default, rename its Contents/Info.plist to Info.plist.disabled. That JDK can still be used when $JAVA_HOME points to it, or explicitly referenced in a script or configuration. It will simply be ignored by system's java command.

System launcher will use the JDK with highest version among those that have an Info.plist file.

When working in a shell with alternate JDK, pick your method among existing answers (jenv, or custom aliases/scripts around /usr/libexec/java_home, etc).


Details of investigation in this gist.


(*) Current answers are either obsolete (no longer valid for macOS El Capitan or Sierra), or only address a single JDK, or do not address the system-wide aspect. Many explain how to change $JAVA_HOME, but this only affects the current shell and what is launched from there. It won't affect an application started from OS launcher (unless you change the right file and logout/login, which is tedious). Same for jenv, it's cool and all, but as far as I can tell it merely changes environment variables, so it has the same limitation.

🌐
DEV Community
dev.to › rithvik78 › change-the-default-java-version-on-macos-3jee
Change the default Java Version on macOS - DEV Community
February 19, 2022 - 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 · Pick the version you want to be the default (1.8.0_202) then:
Discussions

Switching versions of Java on Mac OSX
Not standard, but easier, try using sdkman. More on reddit.com
🌐 r/javahelp
10
1
January 7, 2023
Wrong Java version showing in Terminal on MacOS and Lubuntu.
It is build from OpenJDK as they state on their readme on GitHub. I also installed Liberica JDK 11 on a clean VM and I am getting the a silmilar output on version retrieval as yours. The difference is that mine is a the current version. openjdk version "11.0.12" 2021-07-20 LTS OpenJDK Runtime Environment (build 11.0.12+7-LTS) OpenJDK 64-Bit Server VM (build 11.0.12+7-LTS, mixed mode) More on reddit.com
🌐 r/javahelp
9
3
September 26, 2021
How to choose between multiple JDK's on MacOS?
I have used sdkman - you can use it to manage your sdks like java, Scala, groovy More on reddit.com
🌐 r/java
30
22
August 29, 2021
How do I change the default version of Java that Curse uses to launch Minecraft?
You can't, it will download it again and again and again, I tricked it out by deleting the content of the folder located in Your documents folder\Curse\Minecraft\Install\runtime\jre-x64\1.8.0_25 delete the entire content of this folder not the folder itself and do not rename it cause it would redownload it again, just replace the content with the content of the folder located here : C:\Program Files\Java\jre1.8.0_91 This will trick the game and it works, but I got back to using MultiMC instead, Curse isn't good performance wise, only good for keeping mods up to date, and even sometimes its not up to date either. More on reddit.com
🌐 r/feedthebeast
9
2
May 23, 2016
🌐
Azul
support.azul.com › hc › en-us › articles › 17535521715476-Setting-the-default-Java-version-on-macOS
Setting the default Java version on macOS - Azul Systems
July 19, 2023 - While the JRE or JDK is not part anymore of macOS itself like it was in the past and stopped with Java 6, interfaces, commands and standards to make it simple to use are still integrated by default and continued to be maintained. The following Zulu documentation page explains how to select the default Java version to use in case you have multiple versions installed on your Mac.
🌐
Medium
medium.com › @devkosal › switching-java-jdk-versions-on-macos-80bc868e686a
Switching Java (JDK) Versions on MacOS | by Dev Sharma | Medium
January 10, 2025 - This tutorial will guide you on how to switch JAVA versions on your MAC device. This can be useful for working with apps which don’t work…
🌐
Medium
medium.com › @manvendrapsingh › installing-many-jdk-versions-on-macos-dfc177bc8c2b
Installing & switching between multiple JDK on macOS | by Manvendra P Singh | Medium
February 20, 2023 - alias java-17=”export JAVA_HOME=`/usr/libexec/java_home -v 17`; java -version” alias java-11=”export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version” · We are all done. Just type java-11 or java-17 on your Terminal or iTerm whenever ...
🌐
Medium
medium.com › @haroldfinch01 › step-by-step-guide-installing-and-switching-java-versions-on-mac-osx-f3896b9872f4
Step-by-Step Guide: Installing and Switching Java Versions on Mac OSX | by Harold Finch | Medium
May 21, 2024 - Step-by-Step Guide: Installing and Switching Java Versions on Mac OSX To install Java on macOS and allow for easy switching between different versions, you can use a version manager like jenv. Here's …
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › java › change java version mac
How to Change Java Version in MacOS | Delft Stack
February 2, 2024 - The command output shows all the versions of Java available in the jenv list. Here, 1.8 is the current default Java version. ... > system * 1.8 (set by /Users/Anju/.jenv/version) 1.8.0.202 14 14.0 14.0.1 oracle64-1.8.0.202 oracle64-14.0.1 · Change the Java version from 1.8 to 14.0 globally.
🌐
Oracle
java.com › en › download › help › mac_java_update.html
How do I Update Java for my Mac?
If you choose Skip This Version but later decide to check for an update, you can launch the Java Control Panel by clicking the Java icon in System Preferences. Go to the Update tab to initiate an update check. If you choose Remind Me Later you will be reminded of the update the next time you ...
🌐
Lotharschulz
lotharschulz.info › 2021 › 11 › 30 › how-to-switch-between-java-lts-versions-8-11-and-17-on-mac
How to switch between Java LTS versions 8, 11 and 17 on Mac – Lothar Schulz
javahome() { unset JAVA_HOME export JAVA_HOME=$(/usr/libexec/java_home -v "$1"); java -version } alias j1.8='javahome 1.8' alias j11='javahome 11' alias j17='javahome 17' Make sure to add the javahome function to your shell init file (.zshrc for me) so it is available in all terminal sessions. Now you can change the java version on the terminal using the respective alias:
🌐
W3Docs
w3docs.com › java
How to set or change the default Java (JDK) version on macOS? | W3Docs
To set the default Java (JDK) version on macOS, you can use the /usr/libexec/java_home command line tool to locate the installation path, then configure your shell to use it permanently.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 14515375889170-How-to-Switch-Java-Versions-on-MacOS
How to Switch Java Versions on MacOS – IDEs Support (IntelliJ Platform) | JetBrains
October 18, 2023 - Some information on how to change the Java version in IntelliJ (might be useful for other users finding this post): - Set up the project JDK: https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk - Set java compiler version: https://www.jetbrains.com/help/idea/java-compiler.html#compiler - Set java version for Maven runner: https://www.jetbrains.com/help/idea/maven-support.html#maven_runner_jdk - Set java version for Gradle: https://www.jetbrains.com/help/idea/gradle-jvm-selection.html
🌐
DevQA
devqa.io › switch-between-different-java-versions-mac
How to Switch Between Different Java Versions on Mac
July 4, 2023 - That’s it! You have successfully switched between different versions of Java using jenv. You can set a different version as the default or configure Java version per directory using jenv local command.
🌐
Medium
medium.com › towardsdev › how-to-change-java-jdk-versions-on-macos-erselan-khan-fb04abf57a6a
How to change Java (JDK) Versions on MacOS | Erselan Khan | by Erselan Khan | Towards Dev
May 27, 2024 - Set the JAVA_HOME variable to the desired version by using the path from the previous command: export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.10.jdk/Contents/Home ...
🌐
JavaForge
javaforge.com › home › how to change java version mac
How to change Java version Mac
March 28, 2024 - 3. Apply Changes: Save and exit the editor (in nano, press Ctrl+X), then apply the changes with: ... While the above steps provide a basic framework for switching Java versions, modern development may require more nuanced control. Tools like SDKMAN! or jEnv offer advanced Java version management capabilities, allowing developers to specify Java versions on a per-project basis, enhancing flexibility and reducing the risk of compatibility issues. Effectively managing Java versions on macOS ensures that you can meet the diverse requirements of different applications without compromising on performance or security.
🌐
Kodejava
kodejava.org › how-do-i-set-the-default-java-jdk-version-on-mac-os-x
How do I set the default Java (JDK) version on Mac OS X? - Learn Java by Examples
To make this change permanent you need to set it in your shell init file. For example if you are using bash then you can set the command in the .bash_profile. Add the following lines at the end of the file. # Setting default JDK to version 1.8. export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
🌐
Ninniku IT Hub
ninniku.tw › 首頁 › apple › java version shuffle: how to switch java versions on macos with ease
Java Version Shuffle: How to Switch Java Versions on MacOS with Ease - Ninniku IT Hub
December 14, 2023 - In this article, we’ll provide ... keep things light-hearted! On macOS, you can switch the default Java version by using the java_home command....
🌐
Khuong Dev
dev.ngockhuong.com › posts › change-the-default-java-jdk-version-on-macos
Change the default Java (JDK) version on macOS | Khuong Dev
February 23, 2026 - How to switch between multiple Java JDK versions on macOS using java_home and environment variables. ... Press CTRL+X to exit the editor, Press Y to save your changes.
🌐
GitHub
gist.github.com › krutilin › b32091a785b955e6bd9b6b0ea7bfcc57
How to set or change the default Java (JDK) version on OSX (Mac)? · GitHub
Matching Java Virtual Machines (2): 9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home 1.8.0_152, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home · Choose the version you want to be the default (1.8.0_152 for example) then put following in to your .bash_profile:
🌐
Snyk
snyk.io › blog › installing-and-managing-java-on-macos
Installing and managing Java on macOS | Snyk
October 12, 2023 - Running sdk use instructs SDKMAN! ... or employ a versatile tool like SDKMAN!. To set or change the Java path, edit the environment variable, JAVA_HOME....