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.
How can I downgrade my Java version and R… - Apple Community
How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 - Stack Overflow
How to set or change the default Java (JDK) version on macOS? - Stack Overflow
macos - How to downgrade Java -version in Mac - Stack Overflow
Videos
You don't need to down grade. You can run more than one version of Java on MacOS. You can set the version of your terminal with this command in MacOS.
# List Java versions installed
/usr/libexec/java_home -V
# Java 11
export JAVA_HOME=$(/usr/libexec/java_home -v 11)
# Java 1.8
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
# Java 1.7
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
# Java 1.6
export JAVA_HOME=$(/usr/libexec/java_home -v 1.6)
You can set the default value in the .bashrc, .profile, or .zprofile
This is how I did it. You don't need to delete Java 9 or newer version.
Step 1: Install Java 8
You can download Java 8 from here: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Step 2: After installation of Java 8. Confirm installation of all versions.Type the following command in your terminal.
/usr/libexec/java_home -V
Step 3: Edit .bash_profile
sudo nano ~/.bash_profile
Step 4: Add 1.8 as default. (Add below line to bash_profile file).
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
Now Press CTRL+X to exit the bash. Press 'Y' to save changes.
Step 5: Reload bash_profile
source ~/.bash_profile
Step 6: Confirm current version of Java
java -version
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.gln -s /mnt/star/program/java/jdk-1.8 /mnt/star/program/java/java
Thus/mnt/star/program/java/javais the soft link. - Set
JAVA_HOMEin a start script.
Could use file like/etc/profile.d/eric.sh, or just use~/.bashrc.
e.gJAVA_HOME=/mnt/star/program/java/java - Then open a new bash shell.
java -versionshould 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 ~/.bashrcto make the changes take effect. - Then could switch using the defined commands.
Commands - from above config
Commands:
jv11
Switch to Java 11jv8
Switch to Java 8jvd
Switch to default Java version, which is denoted by_E_JAVA_HOME_Ddefined 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-8orjdk-11
javaI define command alias in
~/.bashrc, but define variable in a separate file.
I am using~/.eric_varto define the variables, and~/.bashrcwill source it (e.gsource $HOME/.eric_var).
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)
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.
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.
That does not seem to be accurate any more.
I just managed to downgrade both JRE and compiler from Java 8 to 7 by looking into /Library/Java/JavaVirtualMachines, and moving away the directory with the highest version number, e.g.
sudo mv jdk1.8.0.jdk ~/Desktop/
That's easy. You have to remove de .jdk directory of the JDK 7 in /Library/Java/JavaVirtualMachines. Now you only have to install the version you like. =)
More info: http://docs.oracle.com/javase/7/docs/webnotes/install/mac/mac-jdk.html#uninstall