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.

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
Java for UniFI Controller
Windows or Linux system for the controller? More on reddit.com
🌐 r/Ubiquiti
10
4
July 12, 2019
Setting JAVA_HOME path
It means that you have not configured the JAVA_HOME environment variable. Using a virtual terminal application, type in: update-alternatives --config java, which will give you the location of JDK listed under Path column. Depending on the installed version of JDK, it will be something like: /usr/lib/jvm/java-14-openjdk-amd64/bin/java If you have installed multiple versions of JDK, all of them will be listed here 2. Using text editor such as nano and terminal emulator, type in: nano /etc/profile.d/jdk_home.sh This will create and open for editing a new script file named jdk_home.sh under the location /etc/profile.d, where you need to type in: export JAVA_HOME= where JDK-Location should not include the /bin/java directories. For example: EXPORT JAVA_HOME=”/usr/lib/jvm/java-14-openjdk-amd64” This will set your environment variable JAVA_HOME The steps above are if you have installed JDK using apt package manager tool, you also have the option of using SDKMAN which will configure all environment variables for you automatically. More on reddit.com
🌐 r/vscode
12
1
February 27, 2020
How to change java versions for different versions without deleting and installing.
Thanks for being a part of | r/Admincraft | ! | We'd love it if you also joined us on Discord! Join thousands of other Minecraft administrators for real-time discussion of all things related to running a quality server. 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/admincraft
21
2
October 1, 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
🌐
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 - Here, We are trying to change the version to Java SE 8 First run /usr/libexec/java_home -V which... Tagged with macos, java, jdk, javase8.
🌐
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 - But, if you want many different versions of java on your Mac, you can install each of them as per the steps above & switch between them as per the steps below · JAVA_HOME is the environment variable that directs java programs to pick the Java location. So to switch between various java versions we need to change the JAVA_HOME value to different locations
🌐
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'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.
Find elsewhere
🌐
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....
🌐
Maarten on IT
maarten.mulders.it › 2017 › 02 › quickly-switch-java-versions-on-macos
Quickly switch Java versions on macOS · Maarten on IT
February 7, 2017 - alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version" alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version" alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"
🌐
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 - How to change default java version on Mac from Java version 11.0.2 to Java version 1.8 to enable a service transition.
🌐
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.
🌐
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 - Remember, efficient management of your java environment mac makes switching between projects smooth and hassle-free. The easiest way to change default java mac is to set the $JAVA_HOME variable.
🌐
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:
🌐
Azul
support.azul.com › hc › en-us › articles › 17535521715476-Setting-the-default-Java-version-on-macOS
Setting the default Java version on macOS – Azul
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 ...
🌐
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`
🌐
Baeldung
baeldung.com › home › java › set java_home variable in windows, mac os x, and linux
Set JAVA_HOME on Windows 7, 8, 10, Mac OS X, Linux | Baeldung
March 26, 2025 - For older versions of OS X, we have to set the exact path to the JDK installation. Open ~/.bash_profile in any editor and add the following: ... Then save and close the file. Open a Terminal and run the source command to apply the changes: ... To set JAVA_HOME globally for all users, the steps are the same as for a single user, but we use the file /etc/profile.
🌐
Medium
medium.com › @annurahar › manage-various-java-versions-on-mac-osx-e6aee82c882c
Manage various Java versions on Mac OSX | by Annu Rahar | Medium
September 16, 2021 - The advantage of a manual install is that the location of the JDK can be placed in a standardised location for Mac OSX. Otherwise, there are easier options such as SDKMAN that also will install other important and common tools for the JVM. SDKMAN is a bit different and handles both the install and the switching. SDKMAN also places the installed JDK’s into its own directory tree, which is typically ~/.sdkman/candidates/java. SDKMAN allows setting a global default version, and a version specific to the current shell.
🌐
Pleets
blog.pleets.org › article › en › install-multiple-versions-of-jdk-on-macos
Install multiple versions of the Java JDK on MacOS.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home · If you want to have a specific version every time you log in, you must modify your terminal configuration file. If you use zsh (the default in MacOS), you must add the variable export instruction to the end of the ~/.zshrc file. It is also quite useful to create an alias to quickly switch between versions so that with just typing a command like the following you can change your version:
🌐
Oracle
java.com › en › download › help › update_runtime_settings.html
Update Java runtime settings on Windows
This could happen when you install other applications that also install Java and the vendors do not want to change your browser settings. However, it is always a good idea to use the latest version of Java to ensure you have the latest security fixes and enhancements. If your browser is not using the latest version you have installed, you will need to enable the latest version of Java in the Java Control Panel. » Windows » Mac OS X ·
🌐
PaperMC
docs.papermc.io › paper › getting-started
Getting started | PaperMC Docs
May 12, 2026 - Paper requires at least Java 25 to run, which is easy to download and install.
🌐
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.
🌐
Minecraft Wiki
minecraft.wiki › w › Tutorial:Setting_up_a_Java_Edition_server
Tutorial: Setting up a Java Edition server – Minecraft Wiki
May 16, 2026 - Type cd (stands for "change directory"), followed by the path to the folder where you placed your server.jar file. For Windows, you can drag the folder into the command-line window to get the path, if you have a GUI open.