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
🌐
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 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
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
Mac/PC Cross Platform?
If you both run the Java client, you can connect to servers and play with each other. If you have the Windows 10 Store app (bedrock), it can't play with Mac on Java. AFIAK, there is no bedrock client for Mac. More on reddit.com
🌐 r/MCPE
4
2
July 29, 2018
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.

🌐
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" Now I can rapidly switch between the three versions I have installed, and I will even get immediate feedback as to whether that succeeded:
🌐
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 - So we need to create a few soft ... Java to be detected by all the Mac programs. Copy-paste the command given in install logs as is ... Now that we have 2 JDKs installed and detected by macOS, let's see how to quickly switch between the two. If you are installing only one version of Java we ...
🌐
Davidsimpson
davidsimpson.me › 2015 › 08 › 12 › switching-java-versions-easily-on-a-mac
Switching Java versions easily on a Mac
I do a lot of development using Java 7 and recently needed to start using Java 8. The quickest way I know to update to the latest Java uses homebrew: brew tap caskroom/cask brew install brew-cask brew cask install java I now need an easy way to switch back and forth between Java 7 & 8. Adding this to your ~/.bash_profile or ~/.profile does the trick: function setjdk() { if [ $# -ne 0 ]; then removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin' if [ -n "${JAVA_HOME+x}" ]; then removeFromPath $JAVA_HOME fi export JAVA_HOME=`/usr/libexec/java_home -v $@` export PATH=$JAVA_HOME/bin:$PATH fi } function removeFromPath() { export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;") } # set your default version setjdk 1.7 – Source.
Find elsewhere
🌐
notiz
notiz.dev › blog › how-to-manage-multiple-java-versions-on-mac
How to manage multiple Java JDK versions on macOS X
April 16, 2020 - The alias exports JAVA_HOME with ... JAVA_HOME=$JAVA_14_HOME' Now, to switch between the Java versions, enter an alias java8 in your terminal....
🌐
Chandra Sekhar Nayak
chansek.com › Switching-java-versions-in-mac
Switching JDK (Java) Versions in Mac OS X - Chandra Sekhar Nayak
November 9, 2020 - Now let’s see how can we switch them easily. And to make all these JDKs accessible, we need to know their paths. To get the each of these JDK paths individually use the below command. ... Now we need to make sure these paths are set to system path with a simple command. And to do that, follow the below steps: Open .bash_profile using any of your favorite editor. (I am using vi) ... Press I to move it to edit mode. ... # Java Homes for different JDK Versions for allowing them to switch easily export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8) export JAVA_11_HOME=$(/usr/libexec/java_home -v11) export JAVA_13_HOME=$(/usr/libexec/java_home -v13) export JAVA_14_HOME=$(/usr/libexec/java_home -v14)
🌐
GitHub
gist.github.com › Ebrahim-Mostafa › d04444b867bc34244feb9e9ff5f0f380
Switching between multiple JDK on macOS · GitHub
Switching between multiple JDK on macOS. GitHub Gist: instantly share code, notes, and snippets.
🌐
Medium
medium.com › @haroldfinch01 › step-by-step-guide-installing-and-switching-java-versions-on-mac-osx-f3896b9872f4
Step-by-Step Guide: Installing and Switching Java Versions on Mac OSX | by Harold Finch | Medium
May 21, 2024 - Step-by-Step Guide: Installing and Switching Java Versions on Mac OSX To install Java on macOS and allow for easy switching between different versions, you can use a version manager like jenv. Here's …
🌐
Snyk
snyk.io › blog › installing-and-managing-java-on-macos
Installing and managing Java on macOS | Snyk
October 12, 2023 - With Java installed, SDKMAN! lets you choose the Java version you wish to use at any given moment. To select a specific Java version, use the following command, replacing the version number: ... SDKMAN! allows for easy switching between Java versions, accommodating the requirements of multiple projects.
🌐
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:
🌐
GitHub
gist.github.com › gramcha › 81dcec3f1e4ce8cffd7f248d3e2a42a7
Managing multiple Java versions in MacOS · GitHub
Execute below command to see available JVM's in machine ... Matching Java Virtual Machines (2): 15.0.1, x86_64: "OpenJDK 15.0.1" /Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home 1.8.0_212, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home /Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
🌐
Minecraft Wiki
minecraft.wiki › w › Tutorial:Setting_up_a_Java_Edition_server
Tutorial: Setting up a Java Edition server – Minecraft Wiki
May 16, 2026 - This tutorial takes you through the steps of setting up your own Java Edition server using the default server software that Mojang Studios distributes free of charge. The software may be installed on most operating systems, including Windows, macOS, and Linux.
🌐
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 ... 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....
🌐
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.
🌐
Charles Schwab
schwab.com › trading › thinkorswim › download
thinkorswim Download | Charles Schwab
You will now have an icon labelled thinkorswim that displays the thinkorswim logo on your desktop. If this is visible, the installation was completed successfully. You may also search your Program Files folder to access thinkorswim. Note: You do not need to install any other software. A Java virtual machine is included with this download.
Top answer
1 of 10
41

I don't think it's possible to switch JRE (runtime environments) see here:

http://docs.oracle.com/javase/7/docs/webnotes/install/mac/mac-jre.html

which states:

Only one JRE can be installed. Installing a JRE removes the previously installed JRE. The JRE version used by the system can be determined in one of two ways:

Workaround:

I had a similar problem like you have with Minecraft with Wuala. Where I needed to run Wuala using Java 1.6 whilst I needed to develop with JDK 1.7 and I managed this by opening the Wuala.app package and changing its startup script in:

/Applications/Wuala.app/Contents/MacOS/wuala

from:

exec java ${VMARGS} -cp "${JAR_DIR}/loader3.jar":/System/Library/Java/ com.wuala.loader3.Loader3 -alternateprogrampath "${JAR_DIR}" -installed $*

to:

/usr/libexec/java_home -v 1.6.0 --exec java ${VMARGS} -cp "${JAR_DIR}/loader3.jar":/System/Library/Java/ com.wuala.loader3.Loader3 -alternateprogrampath "${JAR_DIR}" -installed $*

I.e simply replacing: exec with: /usr/libexec/java_home -v 1.6.0 --exec

This is of course rather involved and will get broken every time wuala autoupdates but otherwise it works.

To use another JDK see here:

http://docs.oracle.com/javase/7/docs/webnotes/install/mac/mac-jdk.html

which states:

To run a different version of Java, either specify the full path, or use the java_home tool: /usr/libexec/java_home -v 1.7.0_06 --exec javac -version

Here is an illustration and examples from my setup:

Oracle JDK installs:

odin:~ geff$ ls -al /Library/Java/JavaVirtualMachines 
total 0
21058660 0 drwxr-xr-x  3 root  wheel  - 102 24 Oct 18:04:33 2012 jdk1.7.0_09.jdk/
21061692 0 drwxr-xr-x  3 root  wheel  - 102 24 Oct 18:06:08 2012 jdk1.7.0_07.jdk/
21042328 0 drwxrwxr-x  3 root  wheel  - 102 20 Apr 06:58:53 2012 1.7.0.jdk/
21031664 0 drwxrwxr-x  7 root  admin  - 238 24 Oct 18:04:16 2012 ../
21042327 0 drwxr-xr-x  5 root  wheel  - 170 24 Oct 18:06:13 2012 ./

Apple supplied JDK:

odin:~ geff$ ls -al /System/Library/Java/JavaVirtualMachines 
total 0
21026468 0 drwxr-xr-x  3 root  wheel  - 102  1 Nov 17:49:02 2011 1.6.0.jdk/
21026436 0 drwxr-xr-x  6 root  wheel  - 204 24 Mar 23:04:06 2012 ../
21026467 0 drwxr-xr-x  3 root  wheel  - 102  1 Nov 17:49:02 2011 ./

This works for me also to use the Apple supplied 1.6 JDK

odin:~ geff$ /usr/libexec/java_home -v 1.6.0_37 --exec java -version
java version "1.6.0_37"
Java(TM) SE Runtime Environment (build 1.6.0_37-b06-434-11M3909)
Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01-434, mixed mode)

Choose between the 1.7 Oracle versions:

Selecting the first Oracle JDK 1.7.0_04

odin:~ geff$ /usr/libexec/java_home -v 1.7.0_04 --exec java -version
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)

Selecting JDK 1.7.0_07

odin:~ geff$ /usr/libexec/java_home -v 1.7.0_07 --exec java -version
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

The default JDK is the highest one:

odin:~ geff$ java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

Defaults to the highest "patch level when using only minor version number:

odin:~ geff$ /usr/libexec/java_home -v 1.7.0 --exec java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
2 of 10
99

One can use the java_home mechanism more conveniently.

Say you have Java 7 as the default:

$ java -version
java version "1.7.0_15" 
…

Now let’s activate Java 6:

$export JAVA_HOME=$(/usr/libexec/java_home -v 1.6)

And there it is:

$ java -version
java version "1.6.0_41"
…

I added these to my .bashrc in order to make things even more convenient:

alias java_ls='/usr/libexec/java_home -V 2>&1 | grep -E "\d.\d.\d[,_]" | cut -d , -f 1 | colrm 1 4 | grep -v Home'

function java_use() {
    export JAVA_HOME=$(/usr/libexec/java_home -v $1)
    export PATH=$JAVA_HOME/bin:$PATH
    java -version
}

java_ls reduces the output of java_home -V to just the version numbers. And java_use is just a shortcut for what we did above. E.g. java_use 1.6 will switch to Java 6.

🌐
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).