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
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 default version of java from my terminal?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
11
2
December 22, 2023
How do I switch between Java versions?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
13
11
March 20, 2023
🌐
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
Matching Java Virtual Machines (3): 9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home 1.8.0_121, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home 1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home · From the list above pick which version you want to be the default JDK. For example, I will choose the 1.8.0_121 version to be my default JDK. To set it run the command below.
🌐
Medium
medium.com › @devkosal › switching-java-jdk-versions-on-macos-80bc868e686a
Switching Java (JDK) Versions on MacOS | by Dev Sharma | Medium
January 10, 2025 - 5. (Optional) To make this the default JDK version, input one of the following in Terminal: # if running zsh which is what recent macs use, open this file open ~/.zshrc# if have an older mac which doesn't use zsh, open this file open ~/.bash_profile · Then, add your Terminal input from step 3 to the end of this file: # SWITCH TO JAVA VERSION 8 export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
🌐
Delft Stack
delftstack.com › home › howto › java › change java version mac
How to Change Java Version in MacOS | Delft Stack
February 2, 2024 - > 14.0.1 (x86_64) "Oracle Corporation" - "Java SE 14.0.1" /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home 1.8.202.08 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home · Now, we get the current default Java version used in the mac.
🌐
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.
Find elsewhere
🌐
Snyk
snyk.io › blog › installing-and-managing-java-on-macos
Installing and managing Java on macOS | Snyk
October 12, 2023 - Finally, we can use SDKMAN! to easily switch between Java versions. Running sdk use instructs SDKMAN! to set the active Java version to 19.0.2: ... Now that we’ve reviewed several methods for installing and managing Java on macOS, let's review the answers to some of the most common questions about the process.
🌐
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
🌐
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 ...
🌐
Oracle
java.com › en › download › help › mac_install.html
How do I install Java for my Mac?
The screen shots and instructions below are for Java 8 Update 311 (8u311). If you are installing another version, make sure you change the version number appropriately. Example: For Java 8 Update 311 (8u311) the file to download is jre-8u311-macosx-x64.dmg
🌐
JavaForge
javaforge.com › home › how to change java version mac
How to change Java version Mac
March 28, 2024 - 1.7.0_80 (x86_64) “Oracle Corporation” – “Java SE 7” /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home ... 3. Set the Default Java Version: Determine the version you wish to use and set it as the default by updating the `JAVA_HOME` environment variable.
🌐
javathinking
javathinking.com › blog › how-to-set-or-change-the-default-java-jdk-version-on-macos
How to Set or Change Default Java (JDK) Version on macOS: Step-by-Step Guide — javathinking.com
Oracle JDK (requires login for older versions). Eclipse Temurin (OpenJDK) (free, open-source). Azul Zulu (OpenJDK distributions). Basic familiarity with the Terminal app (found in Applications/Utilities/). First, identify all JDKs installed on your system. macOS stores JDKs in /Library/Java/JavaVirtualMachines/ by default.
🌐
DevQA
devqa.io › switch-between-different-java-versions-mac
How to Switch Between Different Java Versions on Mac
July 4, 2023 - Run the following command for each installed version: $ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home · Replace the path with the correct path to the Java installation directory of each version.
🌐
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 - Lets use HomeBrew, openjdk and zshrc to install and manage different java / JDK versions on MacOS / OsX
🌐
Medium
medium.com › reachnow-tech › change-default-java-version-on-mac-a7f01647f126
Change default Java version on Mac | by Lothar Schulz | reachnow-tech | Medium
September 6, 2019 - # which java versions installed $ /usr/libexec/java_home -V Matching Java Virtual Machines (2): 11.0.2, x86_64: "OpenJDK 11.0.2" /Library/.../openjdk-11.0.2.jdk/Contents/Home 1.8.0_222, x86_64:"AdoptOpenJDK 8" /Library/.../adoptopenjdk-8.jdk/Contents/Home · # set the new default version export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_222` /Library/.../openjdk-11.0.2.jdk/Contents/Home 1.8.0_222, x86_64:"AdoptOpenJDK 8" /Library/.../adoptopenjdk-8.jdk/Contents/Home
🌐
Oracle
java.com › en › download › help › mac_controlpanel.html
Where is the Java Control Panel on my Mac?
You can view and configure cache files, settings, including file location, as well as delete cache from this setting. Allows you to check for and get the latest available Java version and configure whether you want to automatically update. For more information visit How do I update Java for my Mac?
🌐
Reddit
reddit.com › r/java › how to choose between multiple jdk's on macos?
r/java on Reddit: How to choose between multiple JDK's on MacOS?
August 29, 2021 -

On MacOS, you can run /usr/libexec/java_home to get the current JDK MacOS will use...

On my system, it's:

OpenJDK Runtime Environment Zulu16.32+15-CA (build 16.0.2+7)

But, I have multiple JDK's installed:

/usr/libexec/java_home -V

Matching Java Virtual Machines (2):

16.0.2 (arm64) "Azul Systems, Inc." - "Zulu 16.32.15" /Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home

16.0.2 (x86_64) "Oracle Corporation" - "Java SE 16.0.2" /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home

Does anyone know how (if I can) switch what MacOS will use by default?

So far, if I delete the current default, it'll switch to the one remaining (I've tried this my moving the current JDK to /tmp). But is there a more elegant way to do this?

🌐
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: