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.

🌐
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 ·
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
macos - how to change java version in mac os - Stack Overflow
My Java version is 15.0.1(default), I want to change it to java 8(1.8.0_281). I downloaded java8, and tried several things but the version doesn't change.. change name of /Library/Java/ More on stackoverflow.com
🌐 stackoverflow.com
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 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 - # 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`
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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) ...
🌐
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.
🌐
JRebel
jrebel.com › blog › switching-java-versions-command-line
Switching Java Versions on the Command Line | JRebel by Perforce
August 30, 2017 - It uses sed to replace the argument value with an empty string. Essentially, it removes whatever you pass it from the $PATH. Now with these functions in place you can set the desired versions of Java as active. Save the ~/.bashrc file. If you're doing it in a terminal session, reload it with the source ~/.bashrc.
🌐
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...
Top answer
1 of 1
22

In mac it's pretty simple you can have two or many java versions and based on your requirement you can change that.

I found a very useful tool called jenv.

If you have already had homebrew (a package installer in mac), you can skip this step.

  1. For installing or updating homebrew

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
    
  2. Installing JEnv - java version manager a cool tool helps you to switch between multiple java env on your mac

    brew install jenv
    
  3. Adding jEnv to your path depends on the shell you are using ~/.bash_profile or ~/.bashrc , for zsh it's ~/.zshrc. Add these two lines in your bash profile file

    export PATH="$HOME/.jenv/bin:$PATH"
    eval "$(jenv init -)"
    
  4. Restart the terminal or use source ~/.zshrc

  5. Check if your JEnv is correctly installed

    jenv doctor
    

    you will get the output something like [OK] Jenv is correctly loaded

  6. for installing java 8

      brew install --cask AdoptOpenJDK/openjdk/adoptopenjdk8
    
  7. for installing java 11

    brew install --cask AdoptOpenJDK/openjdk/adoptopenjdk11
    
  8. for list down all the java on your machine

    /usr/libexec/java_home -V
    
  9. Now add these paths to jENV

    Syntax: 
    jenv add <your_jdk_path>
    
    Example:
    jenv add /Library/Java/JavaVirtualMachines/openjdk-14.0.1.jdk/Contents/Home
    
  10. All done. Check your version

    jenv versions
    
  11. For setting system wide java version

     jenv global 14.0
    
  12. For setting locally to terminal

     jenv local 11
    
  13. if you are using maven or gradle for your project

      # ensure that JAVA_HOME is correct
      jenv enable-plugin 
    
      export# make Maven aware of the Java version in use 
      (and switch when your project does)
      jenv enable-plugin maven
    

A reference guide https://www.jenv.be/

🌐
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 - But my Mac OS X Yosemite already installed JDK 1.8 in my system. So, I had to downgrade the JDK to 1.7 to compile the project. Here is what I did. ... Now, open ~/.bash_profile with a text editor. For example, with VIM, you will type vim ~/.bash_profile ... 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 ...
🌐
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:
🌐
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 …
🌐
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...
🌐
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 ...
🌐
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.
🌐
Maarten on IT
maarten.mulders.it › 2017 › 02 › quickly-switch-java-versions-on-macos
Quickly switch Java versions on macOS - Maarten Mulders
February 7, 2017 - How to quickly switch between the two? ... 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"