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.

🌐
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 - Following are the commands we need to add in .bash_profile: export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)export JAVA_11_HOME=$(/usr/libexec/java_home -v11)alias java8='export JAVA_HOME=$JAVA_8_HOME'alias java11='export JAVA_HOME=$JAVA_11_HOME' The first two command are the command we need to run to change java version in or bash shell.
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
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
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 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
People also ask

How can I change the default Java version on macOS?
Use the command export JAVA_HOME=/Library/Java/JavaVirtualMachines//Contents/Home in Terminal, then verify the change with java -version.
🌐
tataneu.com
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
Do changes to the Java version persist after restarting my Mac?
No, unless you update your shell configuration file (e.g., .bash_profile, .zshrc) with the appropriate $JAVA_HOME export command.
🌐
tataneu.com
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
How do I check which Java versions are installed on my Mac?
Run ls /Library/Java/JavaVirtualMachines/ in Terminal to list installed versions.
🌐
tataneu.com
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
🌐
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 - This strategy not only simplifies ... rapid development. Use the command export JAVA_HOME=/Library/Java/JavaVirtualMachines/<version>/Contents/Home in Terminal, then verify the change with 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
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.
🌐
Medium
medium.com › @devkosal › switching-java-jdk-versions-on-macos-80bc868e686a
Switching Java (JDK) Versions on MacOS | by Dev Sharma | Medium
January 10, 2025 - # 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`
Find elsewhere
🌐
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...
🌐
Alvin Alexander
alvinalexander.com › macos › how-to-set-mac-osx-java-version-path
How to set the Java version on Mac OS X (macOS) systems | alvinalexander.com
July 21, 2017 - I don’t remember where I first found this line of code, but if you put it in your Mac OS X ~/.bash_profile file, it’s an easy way to set your Mac Java version:
🌐
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: ... Homebrew may not set up the JAVA_HOME environment variable automatically, so you should add the following lines to your shell profile file (.bash_profile, .zshrc, etc.):
🌐
Mkyong
mkyong.com › home › java › how to set $java_home environment variable on macos
How to Set $JAVA_HOME environment variable on macOS - Mkyong.com
January 19, 2021 - Test with echo $JAVA_HOME. ... 1.2 bash or zsh? On macOS 10.15 Catalina and later, the default Terminal shell switch from the bash (Bourne-again shell) to zsh (Z shell). For bash shell, we can put the environment variables at ~/.bash_profile or ~/.bashrc. For zsh shell, we can put the environment variables at ~/.zshenv or ~/.zshrc. We can print the $SHELL environment variable to determine the current shell you are using. ... Zsh Startup Files. ... 2.1 On Mac OS X 10.5 or later, we can use /usr/libexec/java_home to return the location of the default JDK.
🌐
Medium
bryantson.medium.com › changing-default-java-version-in-mac-os-x-yosemite-470f11a6084b
Changing Default Java Version in Mac OS X (Yosemite) | by Bryant Jimin Son | Medium
May 11, 2020 - Replace the version_of_your_java with the JDK version you want to set. For example, JDK 1.7 will be 1.7. Save the file (for VIM, it is x! in a Command mode) and start a new Terminal window.
🌐
Medium
medium.com › @serkanyldrm_5711 › how-to-set-java-version-in-your-mac-33a15044be12
How To Switch Between Java Versions In Your Mac? | by Serkan Yıldırım, PhD | Medium
September 1, 2020 - Let’s say you have multiple Java versions on your Mac and you want to use different versions in different Java projects. For example; you want to use module system, then you want to set Java 9 or greater. Or, you want to use Lambda calculus but no need for module system, then you want to set Java 8 … · You can paste the below code block to your .bash_profile file in your home directory:
🌐
Tutorials24x7
tutorials24x7.com › java › how-to-switch-java-version-on-mac
How To Switch Java Version On Mac | Tutorials24x7
April 22, 2020 - If you close the terminal and again check the Java version by opening a new terminal, your changes won't be reflected since the Mac system will pick the highest version by default. You can follow the next section to preserve the Java version and switch the Java to default. We can save the default version of Java to the ~/.bash_profile file and execute it to switch to the default version of Java.
🌐
W3Docs
w3docs.com › java
How to set or change the default Java (JDK) version on macOS? | W3Docs
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 11)' >> ~/.zshrc source ~/.zshrc · (For older macOS versions using bash, replace ~/.zshrc with ~/.bash_profile) To verify that the default JDK version has been set correctly, use the java -version command: bash ·
🌐
Mac-Forums
mac-forums.com › apple computing products: › macos - apps and programs
How to make Java 22 default version on High Sierra machine. | Mac-Forums | Fix Mac iPhone iPad | Buying Tips | iOS OS Help
September 8, 2024 - It sounds like Java 21.03 is still showing up because it’s ahead of Java 22 in your `PATH`. First, check if Java 22 is properly installed by looking in `/Library/Java/JavaVirtualMachines/`. Then, update your `PATH` to include Java 22 by editing your shell configuration file (`.bash_profile`, `.zshrc`, etc.) and add the path to Java 22. You can also set Java 22 as the default version using the `java_home` command. Just run `/usr/libexec/java_home -V` to see all installed JDKs and then `export JAVA_HOME=$(/usr/libexec/java_home -v 22)` to switch to Java 22. After making these changes, reload y
🌐
Lotharschulz
lotharschulz.info › 2019 › 08 › 21 › mac-change-default-java-version
Change Java version on Mac – Lothar Schulz
August 21, 2019 - Please make sure you pass an installed java version as the first command-line argument to the shell script. ... See also switch between different jdk versions in gh.com/AdoptOpenJDK repository. ... Something must have changed with Big Sur (11.1). I installed the jdk 8 as shown here and I then set the version with these two lines in my .profile
🌐
Phon
blog.phon.name › java › 2015 › 09 › 05 › switch-java-version-on-mac-os-x.html
Switch Java version on Mac OS X
September 5, 2015 - It’s sufficient to set the JAVA_HOME environment variable but setting the PATH variable to the Java version is better :-) Here is a small snippet for your .bashrc or .bash_profile:
🌐
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.

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

🌐
Cornell Class Wiki
wiki.classe.cornell.edu › Computing › InstallingMultipleVersionsOfJavaOnMac
Installing Multiple Versions of Java on Mac
January 3, 2019 - With this set up the "system" Java version remains Java 8, which will be used by all .app Mac applications. But you can still change to Java 11 in the bash shell.