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

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
java - How to set JAVA_HOME in Mac permanently? - Stack Overflow
To set JAVA_HOME permanently in Mac, I tried following steps. Download and install Java JDK to your Mac. When you install a Java JDK version which will be installed in the following location by default in MAC. More on stackoverflow.com
🌐 stackoverflow.com
Java Configuration Environment in macOS - Apple Community
If you have multiple versions of ... $JAVA_HOME to point to the version you want to use. The specifics on how to do that may vary depending on the version of MacOS you're running and your preferred shell. The default shell in recent OS versions is zsh, so adding: ... to ~/.zshrc would set the default ... More on discussions.apple.com
🌐 discussions.apple.com
August 20, 2025
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
🌐
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
After setting the JAVA_HOME and ... shell you will need to set it again. To make this change permanent you need to set it in your shell init file....
🌐
codippa
codippa.com › home › how to set default java version on mac os
How to set default java version on Mac OS
October 31, 2020 - Note that this will only change the default java for the current terminal session. If you close the terminal and open a new one, you will still see the older java version as the default. 6. To make it permanent default, set JAVA_HOME in .bash_p...
🌐
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.
🌐
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.
🌐
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
openjdk version "17.0.2" 2022-01-18 ... 17.0.2+8, mixed mode, sharing) macOS includes java_home, a utility to locate JDK paths. Use it to temporarily or permanently set the default Java version....
Find elsewhere
🌐
Reddit
reddit.com › r/javahelp › how do i change default version of java from my terminal?
r/javahelp on Reddit: How do I change default version of java from my terminal?
December 22, 2023 -

Currently, when I input "java -version", I get an output of "20.0.1". When I input "/usr/libexec/java_home -V", It says I have 20.0.1, 17.0.9, and 11.0.9.
When I input "export JAVA_HOME=$(/usr/libexec/java_home -v 17.0.9);" to change the version and run "java -version" again, It shows as 17.0.9.
The problem is when I close my terminal and check my java version, it returns back to version 20. How do I permanently change my default java version. Thanks.

🌐
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
🌐
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`
🌐
Quora
quora.com › How-can-I-change-the-default-Java-version-in-Mac-OS
How to change the default Java version in Mac OS - Quora
Answer (1 of 2): The default version can be set by configuring the JAVA_HOME environment variable. On macOS the ‘java’ command, and a few others from the JDK are actually stubs that will launch the correct version of the java tool based on the current setting of JAVA_HOME.
🌐
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:
🌐
javathinking
javathinking.com › blog › how-can-i-change-mac-os-s-default-java-vm-returned-from-usr-libexec-java-home
How to Change Default Java VM on Mac OS: Configure /usr/libexec/java_home to Set Preferred JDK Version — javathinking.com
By following this guide, you can: List installed JDKs with java_home -V. Temporarily switch JDKs for a terminal session. Permanently set a default JDK by persisting JAVA_HOME in your shell config.
🌐
Lotharschulz
lotharschulz.info › 2019 › 08 › 21 › mac-change-default-java-version
Change Java version on Mac – Lothar Schulz
August 21, 2019 - $ brew cask install adoptopenjdk/openjdk/adoptopenjdk8 # 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`
🌐
Techtutelage
techtutelage.net
Install and set default Java JDK version on macOS – TechTutelage
First get a list of all the installed Java versions on you Mac by running the following command ... Matching Java Virtual Machines (2): 19.0.2 (x86_64) "Oracle Corporation" - "Java SE 19.0.2" /Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home 17.0.6 (x86_64) "Oracle Corporation" - "Java SE 17.0.6" /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home · To temporary switch the default Java version to 17 (only for the current terminal session).
🌐
Delft Stack
delftstack.com › home › howto › java › change java version mac
How to Change Java Version in MacOS | Delft Stack
February 2, 2024 - When installing the JDK package on our mac, we have to specify its path as an environment variable to access the Java tool anywhere in the local system. This environment variable is where we tell the version of Java to be used as default.
🌐
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 - First run /usr/libexec/java_home -V to check available Java Virtual Machine: Pick the version you want to be the default (20.0.1) then: export JAVA_HOME="/usr/libexec/java_home -v 20.0.1" Now when you run java -version you will see: Add the · line to your shell’s init file.
🌐
Apple Community
discussions.apple.com › thread › 256120261
Java Configuration Environment in macOS - Apple Community
August 20, 2025 - If you have multiple versions of Java installed, the standard method of switching versions is by defining the environment variable $JAVA_HOME to point to the version you want to use. The specifics on how to do that may vary depending on the version of MacOS you're running and your preferred shell. The default shell in recent OS versions is zsh, so adding: ... to ~/.zshrc would set the default $JAVA_HOME to /usr, but you can point to any other installed version.