Install another JDK

You can install multiple JDKs on your Mac. You can have a JDK for Java 11 as well as a JDK for Java 17. And perhaps you might want another JDK for the new Java 21 that arrived yesterday. These can all live together, side-by-side.

Each JDK is just a folder full of files. You can add JDKs, and delete JDKs, at will.

The environment variable JAVA_HOME is a hint various software as to which JDK you would like to use by default.

You might find a feature within Android Studio to install another JDK. (At least the sibling product, IntelliJ, offers such a feature.)

Configure project

Within your project in Android Studio, you need to set some settings to indicate which of your installed JDKs should be used for compiling, building, and executing your app. See the Android Studio documentation.

SDKMAN!

Personally, I find it easiest to install SDKMAN!, a bunch of shell scripts that manage installing/uninstalling JDKs (and other such kits). This works well on macOS.

SDKMAN! is designed to be utterly simple to use.

On a console such as Terminal.app, execute:

  • sdk list java
    See a list of all JDK products from vendors who chose to submit their data to the SDKMAN! database. From this list you copy the exact name of the JDK product you want to install. The list of products indicates any you have currently installed.
  • sdk install java product_name_goes_here
    Install the JDK product of your choosing. Takes a moment to download and install. At the end, SDKMAN! prompts you to specify whether you want to make this newly installed JDK the default.
  • sdk uninstall java product_name_goes_here
    Removes that particular JDK product from your Mac.
Answer from Basil Bourque on Stack Overflow
Top answer
1 of 1
5

Install another JDK

You can install multiple JDKs on your Mac. You can have a JDK for Java 11 as well as a JDK for Java 17. And perhaps you might want another JDK for the new Java 21 that arrived yesterday. These can all live together, side-by-side.

Each JDK is just a folder full of files. You can add JDKs, and delete JDKs, at will.

The environment variable JAVA_HOME is a hint various software as to which JDK you would like to use by default.

You might find a feature within Android Studio to install another JDK. (At least the sibling product, IntelliJ, offers such a feature.)

Configure project

Within your project in Android Studio, you need to set some settings to indicate which of your installed JDKs should be used for compiling, building, and executing your app. See the Android Studio documentation.

SDKMAN!

Personally, I find it easiest to install SDKMAN!, a bunch of shell scripts that manage installing/uninstalling JDKs (and other such kits). This works well on macOS.

SDKMAN! is designed to be utterly simple to use.

On a console such as Terminal.app, execute:

  • sdk list java
    See a list of all JDK products from vendors who chose to submit their data to the SDKMAN! database. From this list you copy the exact name of the JDK product you want to install. The list of products indicates any you have currently installed.
  • sdk install java product_name_goes_here
    Install the JDK product of your choosing. Takes a moment to download and install. At the end, SDKMAN! prompts you to specify whether you want to make this newly installed JDK the default.
  • sdk uninstall java product_name_goes_here
    Removes that particular JDK product from your Mac.
🌐
Medium
medium.com › miro-engineering › how-to-switch-between-java-lts-versions-8-11-and-17-on-mac-cb6717d1272
How to switch between Java LTS versions 8, 11, and 17 on Mac | by Lothar Schulz | Miro Engineering | Medium
November 25, 2021 - Now you can change the java version on the terminal using the respective alias: $ j17 openjdk version "17.0.1" 2021-10-19 OpenJDK Runtime Environment Homebrew (build 17.0.1+0) OpenJDK 64-Bit Server VM Homebrew (build 17.0.1+0, mixed mode, sharing)$ j11 openjdk version "11.0.12" 2021-07-20 OpenJDK Runtime Environment Homebrew (build 11.0.12+0) OpenJDK 64-Bit Server VM Homebrew (build 11.0.12+0, mixed mode)$ j1.8 openjdk version "1.8.0_312" OpenJDK Runtime Environment (build 1.8.0_312-bre_2021_10_20_23_15-b00) OpenJDK 64-Bit Server VM (build 25.312-b00, mixed mode)
Discussions

How can I downgrade my Java version and R… - Apple Community
I accidentally installed java 7 and I want to downgrade everything to 6. Including the runtime environment. Sorry if I'm stupid and that actually does downgrade everything. (it didn't when I did it) ... How can I uninstall Oracle Java 7 and reinstall Apple's Java 6 on my Mac? More on discussions.apple.com
🌐 discussions.apple.com
How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 - Stack Overflow
If you have multiple Java versions installed on your Mac, here's a quick way to switch the default version using Terminal. In this example, I am going to switch Java 10 to Java 8. $ java -version java version "10.0.1" 2018-04-17 Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10) Java ... More on stackoverflow.com
🌐 stackoverflow.com
How to set or change the default Java (JDK) version on macOS? - Stack Overflow
How can you change the default version of Java on a mac? More on stackoverflow.com
🌐 stackoverflow.com
macos - How to downgrade Java -version in Mac - Stack Overflow
tried the below commands in Mac Terminal and did not update the java version: /usr/libexec/java_home -V export JAVA_HOME=“System/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home” Updated ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › adoptium › adoptium-support › issues › 1009
Downgrading MacOS Arm doesn't work from jdk-17.0.10+7 to jdk-17.0.9+9 · Issue #1009 · adoptium/adoptium-support
January 26, 2024 - Trying to downgrade from jdk-17.0.10+7 to jdk-17.0.9+9 (On Mac Arm, installing JDK arm.) Verified on two different Laptops · Expected behavior: Afterward installing 17.0.9, the following should inform me, that 17.0.9 is installed: /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home/bin/java -version...
Author   adoptium
🌐
Apple Community
discussions.apple.com › thread › 4513733
How can I downgrade my Java version and R… - Apple Community
sudo ln -s /System/Library/Java/Support/CoreDeploy.bundle/Contents/JavaAppletPlugin.plugin/Library/Internet/Plug-Ins/JavaAppletPlugin.plugin · Don't type it in. Just copy it and paste at the Terminal's prompt. ... Still doesn't work. But I know what you're getting at. I've have done that before, but it still doesn't downgrade everything to 6.
🌐
Lotharschulz
lotharschulz.info › 2021 › 11 › 30 › how-to-switch-between-java-lts-versions-8-11-and-17-on-mac
How to switch between Java LTS versions 8, 11 and 17 on Mac – Lothar Schulz
Some of my previous attempts are based on Big Sur 11 (e.g. https://www.lotharschulz.info/2021/01/11/change-default-java-version-on-macos-11-bigsur-persist-it/) . However, I can’t really help with your Mojave apart from upgrading the OS 😉 . ... Turns out that adoptopenjdk offer brew installation of prebuilt binaries, which avoids the need for compilation. I was able to install java 17 on Mojave with brew install — cask temurin (adoptopenjdk has been renamed temurin).
🌐
Lotharschulz
lotharschulz.info › 2019 › 08 › 21 › mac-change-default-java-version
Change Java version on Mac – Lothar Schulz
August 21, 2019 - Please refer to change default version on macOS BigSur: https://www.lotharschulz.info/2021/01/11/change-default-java-version-on-macos-11-bigsur-persist-it/ ... Today I had to downgrade the default java version on Mac because we did start a transition of an existing service.
🌐
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.
Find elsewhere
🌐
Quora
quora.com › How-do-I-downgrade-Java-on-Mac
How to downgrade Java on Mac - Quora
Answer: You can install multiple versions of the JDK simultaneously. Look in /Library/Java/JavaVirtualMachines/ to see which versions are currently installed. There is a folder named CurrentJDK, which is actually a symbolic link to one of the other folders. Remove it and create a new symbolic ...
Top answer
1 of 9
2

Here is how I do it on my Linux (Ubuntu / Mint mate), I guess Mac can do it similarly.


Install & config

Steps:

  • [Basic - part]
  • Download jdk (the .tgz file) by hand.
  • Uncompress & rename properly, at a proper location.
    e.g /mnt/star/program/java/jdk-1.8
  • Make a soft link, which will be changed to switch java version later.
    e.g ln -s /mnt/star/program/java/jdk-1.8 /mnt/star/program/java/java
    Thus /mnt/star/program/java/java is the soft link.
  • Set JAVA_HOME in a start script.
    Could use file like /etc/profile.d/eric.sh, or just use ~/.bashrc.
    e.g JAVA_HOME=/mnt/star/program/java/java
  • Then open a new bash shell. java -version should print the java version.
  • [More version - part]
  • Download & install more Java version, as need, similar as above steps.
    e.g
    /mnt/star/program/java/jdk-11
  • [Switch - part]
  • In ~/.bashrc, define variable for various Java version.
    e.g
    _E_JAVA_HOME_11='/mnt/star/program/java/jdk-11'
    _E_JAVA_HOME_8='/mnt/star/program/java/jdk-8'
    # dir of default version,
    _E_JAVA_HOME_D=$_E_JAVA_HOME_8
  • In ~/.bashrc, define command to switch Java version.
    e.g
    ## switch java version,
    alias jv11="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_11 $JAVA_HOME"
    alias jv8="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_8 $JAVA_HOME"
    # default java version,
    alias jvd="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_D $JAVA_HOME"
    alias jv="java -version"
  • In terminal, source ~/.bashrc to make the changes take effect.
  • Then could switch using the defined commands.

Commands - from above config

Commands:

  • jv11
    Switch to Java 11
  • jv8
    Switch to Java 8
  • jvd
    Switch to default Java version, which is denoted by _E_JAVA_HOME_D defined above.
  • jv
    Show java version.

Example output:

eric@eric-pc:~$ jv
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

eric@eric-pc:~$ jv11
eric@eric-pc:~$ jv
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

eric@eric-pc:~$ jvd
eric@eric-pc:~$ jv
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

eric@eric-pc:~$ 

Mechanism

  • It switch by changing the soft link, which is used as JAVA_HOME.

Tips

  • On my machine when install jdk by hand, I keep the minor version, then make a soft link with the major version but without the minor version.
    e.g
    // this is the actual dir,
    jdk1.8.0_191

    // this is a soft link to jdk1.8.0_191
    jdk-8

    // this is a soft link to jdk-8 or jdk-11
    java

  • I define command alias in ~/.bashrc, but define variable in a separate file.
    I am using ~/.eric_var to define the variables, and ~/.bashrc will source it (e.g source $HOME/.eric_var).

2 of 9
2

A very simple way to change the default Java version on MacOS via HomeBrew:

Start with:

brew install openjdk@17

Once it completes. Run:

sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk

Enter your machine's password and finally run the 2 commands one after another.

export JAVA_HOME=$(/usr/libexec/java_home -v 17) // Specify your own version
export PATH=$JAVA_HOME/bin:$PATH

Check Java Version:

java -version

You're good to go:

openjdk version "17.0.14" 2025-01-21
OpenJDK Runtime Environment Homebrew (build 17.0.14+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.14+0, mixed mode, sharing)
🌐
University of Oregon
service.uoregon.edu › TDClient › 2030 › Portal › KB › ArticleDet
Reverting to a Previous Version of Java (macOS)
This page explains how to uninstall your current version of Java and then install an older version. These instructions focus on Java 8.
🌐
Medium
medium.com › java-mvp › guide-setting-up-and-switching-between-java-11-and-17-on-macos-9caff4c96e2d
Guide: Setting Up and Switching between Java 11 and 17 on macOS | by datatec.studio | Java MVP | Medium
November 25, 2023 - Guide for build and deploy Java Applications. Practice with JakartaEE, Quarkus, Spring-Boot and more. ... This post describes how to set up OpenJDK 11 and OpenJDK 17 on macOS, and it also provides a solution for switching between these two versions.
🌐
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.
🌐
Tutorials24x7
tutorials24x7.com › java › how-to-switch-java-version-on-mac
How To Switch Java Version On Mac | Tutorials24x7
April 22, 2020 - Switch Java versions effortlessly on your Mac with our simple guide. Optimize your development environment and keep projects running smoothly with ease.
🌐
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 …
🌐
Oracle
java.com › mac_uninstall_faq
How do I uninstall Java on my Mac?
Do not attempt to uninstall Java by removing the Java tools from /usr/bin. This directory is part of the system software and any changes will be reset by Apple the next time you perform an update of the OS.
Top answer
1 of 3
3

MacOS comes with versions of Java going back as far as 1.3. See /System/Library/Frameworks/JavaVM.framework/Versions. The only time you need to install from a package is when the latest Java is newer than your OS version and you can't use the Software Update for some reason.

The vendor of your application should really take responsibility, but you can probably fix the problem by editing the .plist of the app. Right click the application and choose Show Package Contents then open the Info.plist file with a text editor. You'll see a Java dictionary in that file and a key JVMVersion. Change the value to 1.5* (1.4+ would mean anything after version 1.4, 1.6* would be any version of 1.6.) That will get your app running in Java 1.5.

However this may not be the whole story. Along side the Info.plist, you'll find MacOS/JavaApplicationStub. This is the actual MacOS X Binary that launches the JVM. The developer may have shipped their app with an old version of this file that is not compatible with your OS. You'll have an up to date copy of this file on your machine already at /System/Library/Frameworks/JavaVM.framework/Versions/Current/Resources/MacOS/JavaApplicationStub. Replace the application's version with this one.

The application might still not run because (for example) it can't load 32-bit native libraries after being launched as 64-bit. In that case you can strip out the 64-bit portion of the Stub with the lipo command: lipo -remove x86_64 JavaApplicationStub -output JavaApplicationStub

Hope this works. And if it does, be sure to let the developer of the app know of course.

2 of 3
1

If you have multiple versions of java installed you can change the order of preference in Applications/Utilities/Java.

Install the version you need and set it to the top preferred version.