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/
2 minutes read
Today I had to downgrade the default java version on Mac because we did start a transition of an existing service. Last time I did install java 1.8 was quite some manual effort (ls.info/…/java-1-8-and-spring-4-0-3-migration/). This time I wanted to use the existing Mac OS capabilities.
My setup was:
- Mac OSX version: 10.14.6
- Brew version: 2.1.10
- Java version: OpenJDK 11.0.2, x86_64
Downgrade to Java version 1.8
Installing java 1.8 worked best with
$ brew cask install adoptopenjdk/openjdk/adoptopenjdk8
found at https://stackoverflow.com/a/56981661
That one installed java 1.8 however the system java version remained untouched.
$ java -version
openjdk version "11.0.2"
In order to set java 1.8 as default, I needed to find out which versions are installed:
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
11.0.2, x86_64: "OpenJDK 11.0.2"
/Library/.../openjdk-11.0.2.jdk/Contents/Home
1.8.0_222, x86_64:"AdoptOpenJDK 8"
/Library/.../adoptopenjdk-8.jdk/Contents/Home
With that, I could set the new default version (in my case as part of .zshrc setup):
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_222`
Finally java -version returned as expected:
$ java -version
openjdk version "1.8.0_222"
found at https://stackoverflow.com/a/24657630
Conclusion
Change default java version on Mac
$ brew cask install adoptopenjdk/openjdk/adoptopenjdk8
# which java versions installed
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
11.0.2, x86_64: "OpenJDK 11.0.2"
/Library/.../openjdk-11.0.2.jdk/Contents/Home
1.8.0_222, x86_64:"AdoptOpenJDK 8"
/Library/.../adoptopenjdk-8.jdk/Contents/Home
# set the new default version
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_222`
Downgrade worked fine for my Mac Catalina. Thank you!
Awesome!
thanks a lot! react native wouldn’t build on android until i reverted default java version from 15 to the one mentioned above. after a lot of failed attempts, you saved me!
I am glad this one is helpful.
do you have any idea why it gets reset back to java 15 after restarting the mac? i’d like it to remember this setting forever
The command
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_222`
is ephemeral if issued only in the terminal. Please consider to persist the JAVA_HOME env in your .bashrc or bash_profile file like this:
I hope that helps.
Add this to your ~/.zshrc
jdk() {
version=$1
export JAVA_HOME=$(/usr/libexec/java_home -v”$version”);
java -version
}
It’ll allow you to simply type ‘jdk 1.8’ at any given time.
Awesome! Please make sure you pass an installed java version as the first command-line argument to the shell script.
You can specify the major version only like:
jdk 1.8
See also switch between different jdk versions in gh.com/AdoptOpenJDK repository.
Something must have changed with Big Sur (11.1). I installed the jdk 8 as shown here and I then set the version with these two lines in my .profile
JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_275`
export JAVA_HOME
But when I run java -version it is still showing the old prior version that is being active…
xxx@yyy ~ % java -version
java version “1.8.0_271”
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
Hmmm. It should be 1.8.0_275 and not 1.8.0_271.
Thanks for the detailed information. I also noticed changes when switching the default java version on macOS BigSur.
Please see how to change the default java version on macOS BigSur as an update of this post.
you’re a lifesaver! worked on macos monterey, and this is the only one that worked. I love you