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 - Press CTRL+X to exit the editor, Press Y to save your changes. ... 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) ... 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:
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 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
Switch JDK version MacOS
Personally I'd recommend using SDKMAN to manage JDK versions. Makes it really easy to switch versions both globally and locally. More on reddit.com
🌐 r/learnjava
7
1
September 25, 2021
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
🌐
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`
🌐
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 ...
🌐
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.
🌐
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 - Open Terminal on your Mac. Type the following command to create or edit the .zshrc file: ... This will open the nano text editor and create or open the .zshrc file in your home directory. Add the following line to the file to set the JAVA_HOME environment variable: export JAVA_HOME=/Library/Java/JavaVirtualMachines/1.8.0_351/Contents/Home · Replace the path with the path to the JDK version you want to use.
🌐
Delft Stack
delftstack.com › home › howto › java › change java version mac
How to Change Java Version in MacOS | Delft Stack
February 2, 2024 - Now, we get the current default Java version used in the mac. We use the command java with -version that returns the version of the default JDK. ... > java version "14.0.1" 2020-04-14 Java(TM) SE Runtime Environment (build 14.0.1+7) Java HotSpot(TM) ...
Find elsewhere
🌐
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. Here's how: Open a Terminal window.
🌐
Bryan Siegel
bryansiegel.com › home › blog › how to change the java version on the mac terminal
How to change the Java version on the Mac terminal - Bryan Siegel
January 7, 2023 - Well it’s super easy. First open up your terminal and type in · /usr/libexec/java_home -V · and you should see the Java versions installed on your machine. Mine looks something like this ·
🌐
Tata Neu
tataneu.com › home › consumer electronics › how to switch java versions on mac: a complete faq guide
How to Switch Java Versions on Mac: A Complete FAQ Guide
April 30, 2025 - The easiest way to change default java mac is to set the $JAVA_HOME variable. Open Terminal and execute: export JAVA_HOME=/Library/Java/JavaVirtualMachines/<version>/Contents/Home.
🌐
Tutorials24x7
tutorials24x7.com › java › how-to-switch-java-version-on-mac
How To Switch Java Version On Mac | Tutorials24x7
April 22, 2020 - Notes: There is no way to set the default JDK on Mac system. It will simply pick the highest version installed on the system. Though, we can switch the active Java on the terminal using the .bash_profile script as shown above.
🌐
Medium
medium.com › @halimmunawar › switching-java-version-easily-on-mac-os-x-bash-shell-bf42d51cf349
Switching Java Version Easily on Mac OS X bash shell | by Halimmunawar | Medium
September 11, 2020 - For other java version, you can use the following command: ... We need to add some alias commands in .bash_profile in such a way that we can easily change java version using single command.
🌐
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 - I'm trying to change the Java version on my Mac M1, because when I run the application in the console, it says that the version I have as default is not the same as the one I'm running the app on. I've already tried accessing these contents here: https://www.ninniku.tw/java-version-shuffle-how-to-switch-java-versions-on-macos-with-ease/#:~:text=Open Terminal on your Mac.&text=This will open the nano,file in your home directory.&text=Replace the path with the,version you want to use.
🌐
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 - We also have /usr/libexec/java_home utility which we will use to move between various versions. For our installation, this is what parameter -v (small case v) gives us ... We can leverage the output of this command to set JAVA_HOME as below, export JAVA_HOME=`/usr/libexec/java_home -v 17 · But the command might be a little long to type, So let's create a one-word alias to replace the whole command. alias java-17=”export JAVA_HOME=`/usr/libexec/java_home -v 17`” · We also need to make sure, every time we open a new terminal, these aliases are available.
🌐
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.
🌐
Snyk
snyk.io › blog › installing-and-managing-java-on-macos
Installing and managing Java on macOS | Snyk
October 12, 2023 - Homebrew is a highly favored package manager in the macOS ecosystem. It supports a vast library of software, including Java, and dramatically simplifies the Java installation process. ... Once the installation is complete, you’re ready to fetch Java. Run the following command in your terminal, replacing 11 with your desired Java version...
🌐
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
First you need to run /usr/libexec/java_home -V command to get the list of installed JDK. The command will print out something like the following depending on the available JDK in your computer.
🌐
DevQA
devqa.io › switch-between-different-java-versions-mac
How to Switch Between Different Java Versions on Mac
July 4, 2023 - To switch between different versions of Java on your Mac, you can use the jenv tool in combination with Homebrew. Here’s a step-by-step guide: 1 - Install jenv using Homebrew by running the following command in Terminal:
🌐
JRebel
jrebel.com › blog › switching-java-versions-command-line
Switching Java Versions on the Command Line | JRebel by Perforce
August 30, 2017 - This guide shows how to switch Java versions on the command line using setjdk, how to clean up the $PATH, and how to set your preferred JDK version. Learn more.