How do I install Java on Mac OSX allowing version switching? - java
I want to install OpenJDK Java on Mac OSX and have it work alongside other JDK's since it is a newer release. Currently, I downloaded the tar.gz and placed it in my path but that is hard to maintain.
The only other install I found that do more things automatically is the install via Homebrew cask. It looks like only the current version too:
brew cask info java
Shows:
java: 13,33:5b8a42f3905b406298b72d750b6919f6
https://openjdk.java.net/
So I can install it from there, but then what? Am I stuck only with the new version?
Note: These solutions work for various versions of Java including Java 8 through Java 17 (the LTS version) and Java 18. This includes alternative JDK's from OpenJDK, Oracle, IBM, Azul, Amazon Correto, Graal and more. Easily work with Java 7, Java 8, Java 9, Java 10, Java 11, Java 12, Java 13, Java 14, Java 15, Java 16, Java 17, Java 18, and the latest Java 19!
You have a few options for how to do the installation as well as manage JDK switching. Installation can be done by Homebrew, SDKMAN, asdf, or a manual install. Switching can be done by SDKMAN, asdf, or manually by setting JAVA_HOME. All of these are described below.
TL;DR - Preferred Methods of Installation
You can install Java using whatever method you prefer including SDKMAN, asdf, Homebrew, or a manual install of the tar.gz file. The advantage of a manual install is that the location of the JDK can be placed in a standardized location for Mac OSX.
However, there are easier options such as SDKMAN and asdf that also will install other important and common tools for the JVM. These two primary options are described here.
Installing and Switching versions with SDKMAN
SDKMAN is a bit different and handles both the install and the switching. SDKMAN also places the installed JDK's into its own directory tree, which is typically ~/.sdkman/candidates/java. SDKMAN allows setting a global default version, and a version specific to the current shell.
Install SDKMAN from https://sdkman.io/install
List the Java versions available to make sure you know the version ID
sdk list java
Install one of those versions, for example, Java 17 LTS:
sdk install java 17-open
Or java 19:
sdk install java 19-open
Make Java 17 the default version:
sdk default java 17-open
Or switch to 17 for the current terminal session:
sdk use java 17-open
When you list available versions for installation using the list command, you will see a wide variety of distributions of Java:
sdk list java
And install additional versions, such as JDK 11 from Amazon:
sdk install java 11.0.14.10.1-amzn
SDKMAN can work with previously installed existing versions. Just do a local install giving your own version label and the location of the JDK:
sdk install java my-local-13 /Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home
And use it freely:
sdk use java my-local-13
SDKMAN will automatically manage your PATH and JAVA_HOME for you as you change versions. And as a note, it installs Java versions to ~/.sdkman/candidates/java/.
More information is available in the SDKMAN Usage Guide along with other SDK's it can install and manage such as Gradle, Maven, Kotlin, Quarkus, Spring Boot, and many others.
Installing and Switching versions with "asdf"
asdf is a version manager that supports installing and managing most languages, frameworks, and developer/devops tools. It has language specific plugins including one for Java.
First, install asdf via https://asdf-vm.com/guide/getting-started.html (read there to setup your shell correctly), or more simply:
brew reinstall asdf
and read the doc for setting up your shell correctly, but if you are using asdf from Homebrew with ZSH you can execute this command to finish setup:
echo -e "\n. $(brew --prefix asdf)/libexec/asdf.sh" >> ${ZDOTDIR:-~}/.zshrc
Then install the Java plugin via https://github.com/halcyon/asdf-java
asdf plugin add java
and read the doc for setting up your shell correctly before continuing. Basically it says to add the following to your ~/.zshrc file (assuming you are not using another shell):
. ~/.asdf/plugins/java/set-java-home.zsh
Now list Java versions:
asdf list-all java
Install your favorite flavor and version:
asdf install java openjdk-17
or install the latest:
asdf install java latest
Other important commands are...
List your installed versions:
asdf list java
Set a global Java version:
asdf global java openjdk-17
Set a local Java version for a directory:
asdf local java openjdk-19
It's that easy! asdf will automatically manage your PATH and JAVA_HOME for you as you change versions. As a note, asdf installs Java versions to ~/.asdf/installs/java.
There are other languages and plugins for asdf here from the repository page: https://github.com/asdf-vm/asdf-plugins
Other Methods of Installation
Install with Homebrew
The version of Java available in Homebrew Cask previous to October 3, 2018 was indeed the Oracle JVM. Now, however, it has now been updated to OpenJDK. Be sure to update Homebrew and then you will see the lastest version available for install.
install Homebrew if you haven't already. Make sure it is updated:
brew update
Add the casks tap:
brew tap homebrew/cask-versions
These casks change their Java versions often, and there might be other taps out there with additional Java versions.
Look for installable versions:
brew search java
or for Eclipse Temurin versions:
brew search temurin
Check the details on the version that will be installed:
brew info java
or for the Temurin version:
brew info temurin
Install a specific version of the JDK such as java11, temurin8, temurin11, temurin17, or just java or temurin for the most current of that distribution. For example:
brew install java
brew install --cask temurin
And these will be installed into /Library/Java/JavaVirtualMachines/ which is the traditional location expected on Mac OSX. There might be additional steps to make the JDK active reported at the end of the install process.
Install manually from OpenJDK download page:
If you need any and every version of Java, this is a good place to look.
Download OpenJDK for Mac OSX from http://jdk.java.net/ (for example Java 17 and Java 19)
Unarchive the OpenJDK tar, and place the resulting folder (i.e. jdk-17.jdk) into your /Library/Java/JavaVirtualMachines/ folder since this is the standard and expected location of JDK installs. You can also install anywhere you want in reality.
Set JAVA_HOME environment variable to point at direction where you unarchived the JDK.
For further information see the answer specific to manual installation. Also see the section below "Switching versions manually" for more information on how to manage multiple manual installations.
Other installation options:
Some other flavours of OpenJDK are:
Azul Systems Java Zulu certified builds of OpenJDK can be installed by following the instructions on their site.
Zulu® is a certified build of OpenJDK that is fully compliant with the Java SE standard. Zulu is 100% open source and freely downloadable. Now Java developers, system administrators, and end-users can enjoy the full benefits of open source Java with deployment flexibility and control over upgrade timing.
Amazon Correto OpenJDK builds have an easy to use an installation package for Java 8, 11, 15, 16, 17, 18, and Java 19. It installs to the standard /Library/Java/JavaVirtualMachines/ directory on Mac OSX.
Amazon Corretto is a no-cost, multiplatform, production-ready distribution of the Open Java Development Kit (OpenJDK). Corretto comes with long-term support that will include performance enhancements and security fixes. Amazon runs Corretto internally on thousands of production services and Corretto is certified as compatible with the Java SE standard. With Corretto, you can develop and run Java applications on popular operating systems, including Linux, Windows, and macOS.
Microsoft Java JDK - certified builds of OpenJDK from Microsoft.
Where is my JDK?!?!
To find locations of previously installed Java JDK's installed at the default system locations, use:
/usr/libexec/java_home -V
Matching Java Virtual Machines (4):
19 (x86_64) "Homebrew" - "OpenJDK 19" /usr/local/Cellar/openjdk/19/libexec/openjdk.jdk/Contents/Home
18.0.1.1 (x86_64) "Homebrew" - "OpenJDK 18.0.1.1" /usr/local/Cellar/openjdk/18.0.1.1/libexec/openjdk.jdk/Contents/Home
17 (x86_64) "Homebrew" - "OpenJDK 17" /usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home
11, x86_64: "Java SE 11" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
1.8.301.09 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home /usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home
You can also report just the location of a specific Java version using -v. For example for Java 17:
/usr/libexec/java_home -v 17
/usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home
Knowing the location of the installed JDK's is also useful when using tools like JEnv, or adding a local install manually to SDKMAN -- and you need to know where to find them.
If you need to find JDK's installed by other tools, check these locations:
SDKMAN installs to ~/.sdkman/candidates/java/
asdf install to ~/.asdf/installs/java
Version Switching
If you are using SDKMAN or asdf you are already covered and can stop reading! Otherwise, here are some options to switch existing VM installations.
Switching versions manually
The Java executable is a wrapper that will use whatever JDK is configured in JAVA_HOME, so you can change that to also change which JDK is in use.
For example, if you installed or untar'd JDK 16 to /Library/Java/JavaVirtualMachines/jdk-16.jdk if it is the highest version number it should already be the default, if not you could simply set:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
And now whatever Java executable is in the path will see this and use the correct JDK.
A simple way to change JDKs is to create a function in your ~/.bashrc or ~/.zshrc file:
jdk() {
version=$1
export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
java -version
}
And then change JDKs simply by:
jdk 1.8
jdk 9
jdk 11
jdk 13
Edits:
removed Jabba and JENV as both appear to have stagnated, issue count is climbing dramatically, and issues/PR's are not being addressed by the maintainers.
This is how I did it.
Step 1: Install Java 11
You can download Java 11 dmg for mac from here: https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html
Step 2: After installation of Java 11. 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 11.0.1 as default. (Add below line to bash_profile file).
export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.1)
to switch to any version
export JAVA_HOME=$(/usr/libexec/java_home -v X.X.X)
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
With Homebrew and jenv:
Assumption: Mac machine and you already have installed homebrew.
Install Java from official Oracle website. You can install multiple versions of JDK. It will install on the following path:
/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/
/Library/Java/JavaVirtualMachines/jdk1.11.0_2.jdk/
/Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/
Without jenv, system will use the java which installed last.
If you want to use/manage multiple version then you can use jenv:
Install and configure jenv:
$ brew install jenv
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(jenv init -)"' >> ~/.zshrc
$ source ~/.zshrc
Add the installed java to jenv:
$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/14.0.2.jdk/Contents/Home
To see all the installed java:
$ jenv versions
Above command will give the list of installed java:
system
1.8
* 1.8.0.291 (set by /Users/lpatel/.jenv/version)
14
14.0
14.0.2
oracle64-1.8.0.291
oracle64-14.0.2
Configure the java version which you want to use:
$ jenv global 1.8.0.291
Manually switching system-default version without 3rd party tools:
As detailed in this older answer, on macOS /usr/bin/java is a wrapper tool that will use Java version pointed by JAVA_HOME or if that variable is not set will look for Java installations under /Library/Java/JavaVirtualMachines/ and will use the one with highest version. It determines versions by looking at Contents/Info.plist under each package.
Armed with this knowledge you can:
control which version the system will use by renaming Info.plist in versions you don't want to use as default (that file is not used by the actual Java runtime itself).
control which version to use for specific tasks by setting $JAVA_HOME
I've just verified this is still true with OpenJDK & Mojave.
On a brand new system, there is no Java version installed:
$ java -version
No Java runtime present, requesting install.
Cancel this, download OpenJDK 11 & 12ea on https://jdk.java.net ;
install OpenJDK11:
$ cd /Library/Java/JavaVirtualMachines/
$ sudo tar xzf ~/Downloads/openjdk-11.0.1_osx-x64_bin.tar.gz
System java is now 11:
$ java -version
openjdk version "11.0.1" 2018-10-16
[...]
Install OpenJDK12 (early access at the moment):
$ sudo tar xzf ~/Downloads/openjdk-12-ea+17_osx-x64_bin.tar.gz
System java is now 12:
$ java -version
openjdk version "12-ea" 2019-03-19
[...]
Now let's "hide" OpenJDK 12 from system java wrapper:
$ cd jdk-12.jdk/Contents/
$ sudo mv Info.plist Info.plist.disabled
System java is back to 11:
$ java -version
openjdk version "11.0.1" 2018-10-16
[...]
And you can still use version 12 punctually by manually setting JAVA_HOME:
$ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
$ java -version
openjdk version "12-ea" 2019-03-19
[...]
If you have multiple versions installed on your machine, add the following in bash profile:
export JAVA_HOME_7=$(/usr/libexec/java_home -v1.7)
export JAVA_HOME_8=$(/usr/libexec/java_home -v1.8)
export JAVA_HOME_9=$(/usr/libexec/java_home -v9)
And add the following aliases:
alias java7='export JAVA_HOME=$JAVA_HOME_7'
alias java8='export JAVA_HOME=$JAVA_HOME_8'
alias java9='export JAVA_HOME=$JAVA_HOME_9'
And can switch to required version by using the alias:
In terminal:
~ >> java7
export JAVA_HOME=$JAVA_7_HOME
Another alternative is using SDKMAN! See https://wimdeblauwe.wordpress.com/2018/09/26/switching-between-jdk-8-and-11-using-sdkman/
First install SDKMAN: https://sdkman.io/install and then...
Install Oracle JDK 8 with: sdk install java 8.0.181-oracle
Install OpenJDK 11 with: sdk install java 11.0.0-open
To switch:
Switch to JDK 8 with sdk use java 8.0.181-oracle
Switch to JDK 11 with sdk use java 11.0.0-open
To set a default:
Default to JDK 8 with sdk default java 8.0.181-oracle
Default to JDK 11 with sdk default java 11.0.0-open
You can use asdf to install and switch between multiple java versions. It has plugins for other languages as well. You can install asdf with Homebrew
brew install asdf
When asdf is configured, install java plugin
asdf plugin-add java
Pick a version to install
asdf list-all java
For example to install and configure adoptopenjdk8
asdf install java adoptopenjdk-8.0.272+10
asdf global java adoptopenjdk-8.0.272+10
And finally if needed, configure JAVA_HOME for your shell. Just add to your shell init script such as ~/.zshrc in case of zsh:
. ~/.asdf/plugins/java/set-java-home.zsh
IMHO, There is no need to install all the additional applications/packages.
Check available versions using the command:
> /usr/libexec/java_home -V
Matching Java Virtual Machines (8):
11, x86_64: "Java SE 11-ea" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
9.0.1, x86_64: "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
1.8.0_181-zulu-8.31.0.1, x86_64: "Zulu 8" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
1.8.0_151, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
1.6.0_65-b14-468, x86_64: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Now if you want to pick Azul JDK 8 in the above list, and NOT Oracle's Java SE 8, invoke the command as below:
> /usr/libexec/java_home -v 1.8.0_181
/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
To pick Oracle's Java SE 8 you would invoke the command:
> /usr/libexec/java_home -v 1.8.0_151
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
As you can see the version number provided shall be the unique set of strings: 1.8.0_181 vs 1.8.0_151
This answer extends on Jayson's excellent answer with some more opinionated guidance on the best approach for your use case:
SDKMAN is the best solution for most users. It's easy to use, doesn't have any weird configuration, and makes managing multiple versions for lots of other Java ecosystem projects easy as well.
Downloading Java versions via Homebrew and switching versions via jenv is a good option, but requires more work. For example, the Homebrew commands in this highly upvoted answer don't work anymore. jenv is slightly harder to setup, the plugins aren't well documented, and the README says the project is looking for a new maintainer. jenv is still a great project, solves the job, and the community should be thankful for the wonderful contribution. SDKMAN is just the better option cause it's so great.
Jabba is written is a multi-platform solution that provides the same interface on Mac, Windows, and PC (it's written in Go and that's what allows it to be multiplatform). If you care about a multiplatform solution, this is a huge selling point. If you only care about running multiple versions on your Mac, then you don't need a multiplatform solution. SDKMAN's support for tens of popular SDKs is what you're missing out on if you go with Jabba.
Managing versions manually is probably the worst option. If you decide to manually switch versions, you can use this Bash code instead of Jayson's verbose code (code snippet from the homebrew-openjdk README:
jdk() {
version=$1
export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
java -version
}
Jayson's answer provides the basic commands for SDKMAN and jenv. Here's more info on SDKMAN and more info on jenv if you'd like more background on these tools.
To stay with a specific major release, activate the AdoptOpenJDK tap with brew tap and then install the desired version with brew cask install:
$ brew tap AdoptOpenJDK/openjdk
$ brew cask install <version>
To install AdoptOpenJDK 14 with HotSpot, run:
$ brew tap AdoptOpenJDK/openjdk
$ brew cask install adoptopenjdk14
You can install the JDK version in any way you want.
Homebrew
SDK man
Manually
Then, I recommend using JENV to switch between different versions and use the JDK you need globally or locally.
You can find more details about how to install and use JENV here ->
https://blog.adamgamboa.dev/2021/06/17/using-jenv-to-switch-jkd-versions/
Related
Upgrading Jenkins Java version from 8 to 11
I'd like to upgrade java from 8 to 11 as recommended for Jenkins 2.303.1, I'm reading the guide in https://www.jenkins.io/doc/administration/requirements/upgrade-java-guidelines/, but I don't know what to do in step: Use a package manager to install the new JVM. Make sure the default JVM is the newly installed version. If it is not, use the correct java command in the Jenkins startup scripts (/etc/default/jenkins or /etc/init.d/jenkins). does it mean install Java 11 and set java 11 is default one?
Note: since June 2022, you do not have a choice anymore. JDK 11 will need to be the one running Jenkins. It does not have to be the "default" on your system. Only the one selected in the command use to run Jenkins. Jenkins requires Java 11 (Basil Crow) Beginning with Jenkins 2.357 (released on June 28, 2022) and the forthcoming September LTS release, Jenkins requires Java 11. Additionally, beginning with Jenkins 2.355 (released on June 14, 2022) and Jenkins 2.346.1 LTS (released on June 22, 2022), Jenkins supports Java 17. Plugins have already been prepared in JENKINS-68446. Use the Plugin Manager to upgrade all plugins before and after upgrading to Jenkins 2.357. Warning regarding JAXB Prior to Java 11, Java Architecture for XML Binding (JAXB) was part of the Java Platform, and one could use it without adding a third-party dependency. Beginning with Java 11, JAXB is no longer a part of the Java Platform and requires adding a third-party dependency. Thanks to work done several years ago by Baptiste Mathus and others, a JAXB Jenkins plugin is available, which provides the JAXB library to Jenkins plugins in the form of a plugin-to-plugin dependency. The vast majority of plugins have already been prepared to support Java 11 via the JAXB plugin in JENKINS-68446. Jenkins users need only upgrade plugins to compatible versions as documented in the Released As field in Jira. It is critical to use the Plugin Manager to upgrade all plugins before and after upgrading to Jenkins 2.357. Failure to upgrade plugins to compatible versions may result in ClassNotFoundException, NoClassDefFoundError, or other low-level Java errors. Upgrade Docker The official Jenkins Docker images have been based on Java 11 for many months, with Java 8 available as a fallback and Java 17 available in preview mode. Beginning with Jenkins 2.357, the Java 8 images will be retired and the Java 17 images will transition from preview to general availability (GA). Users of the official Jenkins Docker images need not install or configure Java on their own, as it comes preinstalled in the image. OS packages Users of the official Jenkins OS packages for Debian, Red Hat, and SUSE Linux distributions should note that these packages are agnostic to the Java vendor. > In other words, you must bring your own Java package. One straightforward way to do this is to install Java 11 from your Linux distribution, as described on the package download site Recommenced Garbage collection options -XX:+AlwaysPreTouch -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${PATH} -XX:+UseG1GC -XX:+UseStringDeduplication -XX:+ParallelRefProcEnabled -XX:+DisableExplicitGC -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xlog:gc*=info,gc+heap=debug,gc+ref*=debug,gc+ergo*=trace,gc+age*=trace:file=${PATH}/gc.log:utctime,pid,level,tags:filecount=2,filesize=100M -XX:ErrorFile=${PATH}/hs_err_%p.log -XX:+LogVMOutput -XX:LogFile=${PATH}/jvm.log Agents For best results, it is recommended to run agents with the same version of Java as the version used on the controller. Use the Versions Node Monitors plugin to verify that agents are running a compatible version of Java. Running the Jenkins remoting process on an agent with Java 11 or 17 does not imply that you need to run your builds with the same version of Java. You can continue to use any desired version of Java for individual builds.
I opted to not install Java on the system, and followed the below approach: Go to Java 11 Downloads (registration on oracle.com required) Download the latest Java 11 Compressed Archive (zip) file. At the time of writing: jdk-11.0.15_windows-x64_bin.zip Unzip this on your build server, keep the path where you extracted to for step 5 Backup Jenkins Home, especially your jenkins.xml file (same folder as jenkins.war). In Jenkins.xml, update Executable to extracted path, e.g. C:\Java\jdk-11.0.11\bin\java.exe Restart Jenkins Service
However you have (OpenJDK) 11 installed, as long as you have either JAVA_HOME=/path/to/jdk11 and PATH=$JAVA_HOME/bin:$PATH or explicitly run /path/to/jdk11/bin/java -jar jenkins.war or if you type java -version and it returns "11", you are fine. Note: that is to launch jenkins. You can set the JDK available to your jenkins jobs from within Manage | Global Tools Configurationtion. 2nd Note (updated): the documentation has been updated (2021-09-16) ... The default Docker image jdk as of 2.303.1 is now 11, so you don't need to append ”-jdk11", rather users must append "-jdk8" if users want to stick to 8.
if you have Centos you can do the following(i have done this for Centos7 and Jenkins 2.367) sudo yum install java-11-openjdk yum install output if have issues with repos and you want do this manually you get the rpm's that you want and then : first update the dependency . sudo rpm -Uvh tzdata-java-2020a-1.el7.noarch.rpm and then sudo rpm -Uvh java-11-openjdk-headless-11.0.8.10-1.el7.x86_64.rpm sudo rpm -Uvh java-11-openjdk-11.0.8.10-1.el7.x86_64.rpm then you can give sudo update-alternatives --config java to find the java path of java 11 --> /usr/lib/jvm/java-11-openjdk-11.0.8.10-1.el7.x86_64/bin/java it should not be the default, if you don't want to ,you can just copy it and use it to jenkins nodes Java path at configure nodes jenkins JavaPath
You have two options: You can globally install Java 11 on your system, for example using a package manager like apt. You would then make Java 11 your default Java. The other option is to manually install Java, for example OpenJDK, and extract it to, for example, /opt/java and adjust the Jenkins startup script to use that specific Java installation.
I just upgraded a Jenkins installed from apt packages (from: http://pkg.jenkins-ci.org/debian/) with the following steps (inspired by: https://www.jenkins.io/doc/book/installing/linux/) First, install java 11: sudo apt install openjdk-11-jre Then check java -version which may still show openjdk version "1.8.[x]" If so, do choose a java-11 alternative with: update-alternatives --config java Then java -version should show openjdk version "11.0.[x]" Then you can do: systemctl restart jenkins
docker exec -it <container_id> /bin/bash If permission denied error, then, docker exec -u 0 -it <container_id> /bin/bash sudo apt-get update sudo apt-get install openjdk-11-jdk export PATH=$JAVA_HOME/bin:$PATH java -version
Switching Java 11 for Java 8 on the server
So I've seen a bunch of tutorials on switching from Java 8 to Java 11, but I actually need to do the opposite. And I am doing it on a server, so I can use only bash. I am pretty clueless of how to do it. Any help?
You can run this command and chose version java: sudo update-alternatives --config java
This is what I do on my Debian system: alias j8='export JAVA_HOME=/usr/lib/jvm/bellsoft-java8-full-amd64;export PATH=$JAVA_HOME/bin:$PATH' This will ensure all executables are of the same version, and is a lot easier than update-alternatives, even if it did work (see my comments below another answer) or update-java-alternatives and is less error-prone
I use this tool SdkMan Install, SkMan manual skd list java print the versions of java available, and you can install it with comands like sdk install java 8.0.292.hs-adpt After you install a java version with sdkman set the java Home with export JAVA_HOME=~/.sdkman/candidates/java/current export PATH=$JAVA_HOME/bin:$PATH you can install many java versions as you want and set your default version with sdk default java 8.0.292.hs-adpt If you use maven or gradle install them from sdkman, because in this way autodetects the version switching. If you want to switch java versions only for specific projects check the env and .sdkmanrc part in the manual. It switches versions when you cd into a directory and go back to default when you cd out.
Not able to change the JDK in Mac OS using Jenv
I am using Amazon corretto JDK and earlier had only 1.8 installed in my Mac OS, yesterday I installed Amazon corretto JDK 11 using the Mac OS package installer and after that it changed my default java version to 11. I followed highly rated medium blog on Jenv to manage multiple version on Mac OS but still my default java version is not switching back to 1.8 and didn't get any error while following stackoverflow answer or medium post. command using Jenv jenv versions system 1.8 * 1.8.0.252 (set by /Users/my user/.jenv/version) // note `*` that should tell the current version IMO 11 11.0 11.0.7 corretto64-1.8.0.252 corretto64-11.0.7 output of java -version java --version openjdk 11.0.7 2020-04-14 LTS OpenJDK Runtime Environment Corretto-11.0.7.10.1 (build 11.0.7+10-LTS) OpenJDK 64-Bit Server VM Corretto-11.0.7.10.1 (build 11.0.7+10-LTS, mixed mode) Note: I have multiple apps which used different JDK versions and I don't want to use the alias way of managing the JDK version, I am interested in Jenv where I can set my JDK version globally(1.8 in my case) and locally(based on the JDK particular app uses).
The problem that you are seeing is because jenv is not setup properly for your terminal. Depending on your terminal, you should look at your .bash_profile or .zshrc and ensure that the following lines exist: export PATH="$HOME/.jenv/bin:$PATH" eval "$(jenv init -)" You can test to see if jenv has been initialized properly in your terminal by running which java. If jenv is initialized properly you will see something like: /Users/your-username/.jenv/shims/java If you don't see this, double check .bash_profile or .zshrc as described above. I also recommend that you place these at the end of the file so that you can be sure that jenv gets added onto the start of your PATH. The magic behind jenv is jenv init. When you open your terminal this runs and updates your PATH to look at ~/.jenv/shims which contains a shim (shell script) which resolves your desired version of Java when you run java or other JRE/JDK commands.
Try adding this to your .bash_profile or .zshrc or .bashrc whatever is relevant to your macOS. jdk() { version=$1 export JAVA_HOME=$(/usr/libexec/java_home -v"$version"); java -version } Then, restart terminal and you can switch java versions like: jdk 1.8 jdk 11
Behind the scenes jenv uses the /Library/Java/JavaVirtualMachines directory. Then you could also type /usr/libexec/java_home -V to show all the available JDKs and analyse your issue: Matching Java Virtual Machines (4): 11.0.2_2-OracleJDK, x86_64: "Java SE 11.0.2" /Library/Java/JavaVirtualMachines/OracleJDK-jdk-11.0.2.jdk/Contents/Home 11.0.2_1-OpenJDK, x86_64: "OpenJDK 11.0.2" /Library/Java/JavaVirtualMachines/OpenJDK-jdk-11.0.2.jdk/Contents/Home 1.8.0_11, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home 1.7.0_45, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home /Library/Java/JavaVirtualMachines/OracleJDK-jdk-11.0.2.jdk/Contents/Home In this exemple export JAVA_HOME=$(/usr/libexec/java_home -v 1.8) set the JDK 1.8 version. Thus, you even could define the following aliases: java11_OpenJDK_export='export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.2_1-OpenJDK)' java11_OracleJDK_export='export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.2_2-OracleJDK)' java7_export='export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)' java8_export='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)' NB: The version to use after java_home -v is defined in each JDK installation in the Contents/Info.plist file section JVMVersion. I changed it, to easily switch between the OpenJDK and the Oracle JDK: $ tail -5 /Library/Java/JavaVirtualMachines/OpenJDK-jdk-11.0.2.jdk/Contents/Info.plist | head -2 <key>JVMVersion</key> <string>11.0.2_1-OpenJDK</string> $ tail -5 /Library/Java/JavaVirtualMachines/OracleJDK-jdk-11.0.2.jdk/Contents/Info.plist | head -2 <key>JVMVersion</key> <string>11.0.2_2-OracleJDK</string> So, in your case as you have two 1.8 versions (1.8 and 1.8.0.252). It is more than likely that you should update their Contents/Info.plist file section JVMVersion.
java's path still /usr/bin/java after brew cask install java
I installed java with homebrew, using the command brew cask install java. After successfully installing, I typed which java, and the path showed to be /usr/bin/java When I typed brew cask info java, this showed up: java: 1.8.0_102-b14 Java Standard Edition Development Kit /usr/local/Caskroom/java/1.8.0_102-b14 (227.5M) https://github.com/caskroom/homebrew-cask/blob/master/Casks/java.rb Contents JDK 8 Update 102.pkg (pkg) Caveats This Cask makes minor modifications to the JRE to prevent issues with packaged applications, as discussed here: If your Java application still asks for JRE installation, you might need to reboot or logout/login. Installing this Cask means you have AGREED to the Oracle Binary Code License Agreement for Java SE at Did I forget to link something?
/usr/bin/java is a symlink. To see where it points, type ls -la /usr/bin/java brew cask is installing into /Library/Java/JavaVirtualMachines/jdk1.8.0_something.jdk/Contents/Home If the previous command ls -la is matching this, you are good. If not, it means that you previously had another java installation. In this case, you may want to use a tool like jenv to switch between your multiple installs.
To add to #djangofan .. when I did a brew reinstall, I noticed the following message: ==> Pouring openjdk--17.0.1_1.big_sur.bottle.tar.gz ==> Caveats For the system Java wrappers to find this JDK, symlink it with sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk openjdk is keg-only, which means it was not symlinked into /usr/local, because macOS provides similar software and installing this software in parallel can cause all kinds of trouble. If you need to have openjdk first in your PATH, run: echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> /Users/johndoe/.bash_profile For compilers to find openjdk you may need to set: export CPPFLAGS="-I/usr/local/opt/openjdk/include" To his point, I did not install via cask. I assume that when you did the install you got a similar message that you simply did not notice.
Mac OS X and multiple Java versions
How can I install an additional java on MacOS? I installed jdk8 and that works fine. But now I need a jdk7 installation for development purposes. When trying to install the old version via DMG file, i get a warning, that there is already a newer version of java installed and the installer quits. /usr/libexec/java_home -verbose Matching Java Virtual Machines (1): 1.8.0_20, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home How to install jdk7 in addition to this one?
The cleanest way to manage multiple java versions on Mac is to use Homebrew. And within Homebrew, use: homebrew-cask to install the versions of java jenv to manage the installed versions of java As seen on http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html , these are the steps to follow. install homebrew install homebrew jenv install homebrew-cask install a specific java version using cask (see "homebrew-cask versions" paragraph below) add this version for jenv to manage it check the version is correctly managed by jenv repeat steps 4 to 6 for each version of java you need homebrew-cask versions Add the homebrew/cask-versions tap to homebrew using: brew tap homebrew/cask-versions Then you can look at all the versions available: brew search java Then you can install the version(s) you like: brew install --cask java7 brew install --cask java6 And add them to be managed by jenv as usual. jenv add <javaVersionPathHere> I think this is the cleanest & simplest way to go about it. Another important thing to note, as mentioned in Mac OS X 10.6.7 Java Path Current JDK confusing : For different types of JDKs or installations, you will have different paths You can check the paths of the versions installed using /usr/libexec/java_home -V, see How do I check if the Java JDK is installed on Mac? On Mac OS X Mavericks, I found as following: Built-in JRE default: /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home JDKs downloaded from Apple: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/ JDKs downloaded from Oracle: /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home Resources Removing Java 8 JDK from Mac http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html http://sourabhbajaj.com/mac-setup/index.html http://brew.sh https://github.com/Homebrew/homebrew/tree/master/share/doc/homebrew#readme http://sourabhbajaj.com/mac-setup/Homebrew/README.html "brew tap” explained https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/brew-tap.md “brew versions” explained Homebrew install specific version of formula? and also https://github.com/Homebrew/homebrew-versions https://github.com/caskroom/homebrew-cask “cask versions”, similar to “brew versions”, see https://github.com/caskroom/homebrew-versions and also https://github.com/caskroom/homebrew-cask/issues/9447 http://www.jenv.be https://github.com/gcuisinier/jenv
Uninstall jdk8, install jdk7, then reinstall jdk8. My approach to switching between them (in .profile) : export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7) export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8) export JAVA_9_HOME=$(/usr/libexec/java_home -v9) alias java7='export JAVA_HOME=$JAVA_7_HOME' alias java8='export JAVA_HOME=$JAVA_8_HOME' alias java9='export JAVA_HOME=$JAVA_9_HOME' #default java8 export JAVA_HOME=$JAVA_8_HOME Then you can simply type java7 or java8 in a terminal to switch versions. (edit: updated to add Dylans improvement for Java 9)
For macOS Sierra 420 This guide was cobbled together from various sources (replies above as well as other posts), and works perfect. 0. If you haven't already, install homebrew. See https://brew.sh/ 1. Install jenv brew install jenv 2. Add jenv to the bash profile if which jenv > /dev/null; then eval "$(jenv init -)"; fi 3. Add jenv to your path export PATH="$HOME/.jenv/shims:$PATH" 4. Tap "homebrew/cask-versions" FYI: "Tap" extends brew's list of available repos it can install, above and beyond brew's default list of available repos. brew tap homebrew/cask-versions 5. Install the latest version of java brew install java --cask 6. Install java 6 (or 7 or 8 whatever you need) brew install java6 --cask #brew install java7 --cask #brew install java8 --cask ? Maybe close and restart Terminal so it sees any new ENV vars that got setup. 7. Review Installations All Java version get installed here: /Library/Java/JavaVirtualMachines lets take a look. ls -la /Library/Java/JavaVirtualMachines 8. Add each path to jenv one-at-a-time. We need to add "/Contents/Home" to the version folder. WARNING: Use the actual paths on your machine... these are just EXAMPLE's jenv add /Library/Java/JavaVirtualMachines/1.6.0___EXAMPLE___/Contents/Home jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk___EXAMPLE___/Contents/Home 9. Check if jenv registered OK jenv versions 10. Set java version to use (globably) Where XX matches one of the items in the versions list above. jenv global XX Check java version java -version Check jenv versions Should also indicate the current version being used with an asterisk. jenv versions DONE Quick future reference To change java versions ... See the list of available java versions jenv versions ... then, where XX matches an item in the list above jenv global XX
SDKMAN! is a great tool for using multiple versions of Java, Gradle, Groovy, Kotlin, and other JVM tools on Mac OS. Installation and usage doc are easily found on the main site. (I have no affiliation, just a happy user). As an example usage, if I type the following in a Terminal window, there is a list of available Java SDK versions (edited for brevity): $ sdk list java Available Java Versions + 9ea170 > + 8u131 7u141-zulu Here + denotes that the version is installed. > denotes which version is currently in use. To install a version: $ sdk install java 7u141-zulu To use a version in this Terminal window: $ sdk use java 9ea170
First, you need to make certain you have multiple JAVA versions installed. Open a new Terminal window and input: /usr/libexec/java_home -V Your output should look like: Matching Java Virtual Machines (2): 11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home 1.8.0_201, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home Note that there are two JDKs available. If you don’t notice the Java version you need to switch to, download and install the appropriate one from here https://www.oracle.com/java/technologies/downloads/ (JDK 8 is represented as 1.8) . Once you have installed the appropriate JDK, repeat this step. Take note of the JDK version you want to switch to. For example, “11.0” and “1.8” are the JDK versions available in the example above. Switch to the desired version. For example, if you wish to switch to JDK 8, input the following line: export JAVA_HOME=/usr/libexec/java_home -v 1.8 For 11.0, switch “1.8” with “11.0” 4. Check your JDK version by inputting into Terminal: java -version If you have followed all the steps correctly, the JDK version should correlate with the one you specified in the last step. 5. (Optional) To make this the default JDK version, input the following in Terminal: open ~/.bash_profile Then, add your Terminal input from step 3 to this file: SWITCH TO JAVA VERSION 8 export JAVA_HOME=`/usr/libexec/java_home -v 1.8` Save and close the file.
As found on this website So Let’s begin by installing jEnv Run this in the terminal brew install https://raw.github.com/gcuisinier/jenv/homebrew/jenv.rb Add jEnv to the bash profile if which jenv > /dev/null; then eval "$(jenv init -)"; fi When you first install jEnv will not have any JDK associated with it. For example, I just installed JDK 8 but jEnv does not know about it. To check Java versions on jEnv At the moment it only found Java version(jre) on the system. The * shows the version currently selected. Unlike rvm and rbenv, jEnv cannot install JDK for you. You need to install JDK manually from Oracle website. Install JDK 6 from Apple website. This will install Java in /System/Library/Java/JavaVirtualMachines/. The reason we are installing Java 6 from Apple website is that SUN did not come up with JDK 6 for MAC, so Apple created/modified its own deployment version. Similarly install JDK7 and JDK8. Add JDKs to jEnv. JDK 6: JDK 7: JDK 8: Check the java versions installed using jenv So now we have 3 versions of Java on our system. To set a default version use the command jenv local <jenv version> Ex – I wanted Jdk 1.6 to start IntelliJ jenv local oracle64-1.6.0.65 check the java version java -version That’s it. We now have multiple versions of java and we can switch between them easily. jEnv also has some other features, such as wrappers for Gradle, Ant, Maven, etc, and the ability to set JVM options globally or locally. Check out the documentation for more information.
In the same spirit than #Vegard (lightweight): Install the wanted JDKs with Homebrew Put this jdk bash function and a default in your .profile jdk() { version=$1 export JAVA_HOME=$(/usr/libexec/java_home -v"$version"); java -version } export JAVA_HOME=$(/usr/libexec/java_home -v11); # Your default version and then, to switch your jdk, you can do jdk 9 jdk 11 jdk 13 Based on https://github.com/AdoptOpenJDK/homebrew-openjdk.
Manage multiple java version in MAC using jenv Install homebrew using following command /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" install jenv and activate jenv brew install jenv echo 'eval "$(jenv init -)"' >> ~/.bash_profile tap cask-versions brew tap homebrew/cask-versions search available java version that can be installed brew search java E.g. to install java6 use following command brew install cask java6 Add multiple versions of java in jenv jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home jenv add /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home Note:- if you get error like ln: /Users/<username>/.jenv/versions/oracle64-1.8.0.231: No such file or directory, then run following:- mkdir -p /Users/<username>/.jenv/versions/oracle64-1.8.0.231 Rehash jenv after adding jdk’s jenv rehash List known versions of java to jenv jenv versions Set default version jenv global oracle64-1.8.0.231 Change java version for a project jenv local oracle64-1.6.0.65 set JAVA_HOME with the same version as jenv jenv exec bash echo $JAVA_HOME
I find this Java version manager called Jabba recently and the usage is very similar to version managers of other languages like rvm(ruby), nvm(node), pyenv(python), etc. Also it's cross platform so definitely it can be used on Mac. After installation, it will create a dir in ~/.jabba to put all the Java versions you install. It "Supports installation of Oracle JDK (default) / Server JRE, Zulu OpenJDK (since 0.3.0), IBM SDK, Java Technology Edition (since 0.6.0) and from custom URLs.". Basic usage is listed on their Github. A quick summary to start: curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh # install Oracle JDK jabba install 1.8 # "jabba use 1.8" will be called automatically jabba install 1.7 # "jabba use 1.7" will be called automatically # list all installed JDK's jabba ls # switch to a different version of JDK jabba use 1.8
I am using Mac OS X 10.9.5. This is how I manage multiple JDK/JRE on my machine when I need one version to run application A and use another version for application B. I created the following script after getting some help online. #!bin/sh function setjdk() { if [ $# -ne 0 ]; then removeFromPath '/Library/Java/JavaVirtualMachines/' if [ -n "${JAVA_HOME+x}" ]; then removeFromPath $JAVA_HOME fi export JAVA_HOME=/Library/Java/JavaVirtualMachines/$1/Contents/Home export PATH=$JAVA_HOME/bin:$PATH fi } function removeFromPath() { export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;") } #setjdk jdk1.8.0_60.jdk setjdk jdk1.7.0_15.jdk I put the above script in .profile file. Just open terminal, type vi .profile, append the script with the above snippet and save it. Once your out type source .profile, this will run your profile script without you having to restart the terminal. Now type java -version it should show 1.7 as your current version. If you intend to change it to 1.8 then comment the line setjdk jdk1.7.0_15.jdk and uncomment the line setjdk jdk1.8.0_60.jdk. Save the script and run it again with source command. I use this mechanism to manage multiple versions of JDK/JRE when I have to compile 2 different Maven projects which need different java versions.
Jenv on Mac Sierra: if not working after install, do this bug fix to add java executable to path export PATH="$HOME/.jenv/shims:$PATH" even though eval "$(jenv init -)" could do this job. The reason is /bin folder is not there anymore as describe in it's homepage, but shim folder is used as /bin instead. Make sure ~/.jenv is there which java may print /Library/... jenv global 1.8 jenv shell 1.8 Eventually, which java gives you: /Users/xxxx/.jenv/shims/java
I answer lately and I really recommand you to use SDKMAN instead of Homebrew. With SDKMAN you can install easily different version of JAVA in your mac and switch from on version to another. You can also use SDKMAN for ANT, GRADLE, KOTLIN, MAVEN, SCALA, etc... To install a version in your mac you can run the command sdk install java 15.0.0.j9-adpt
I know that this question already have a lot of answers, but I want to share my solution only using Temurin and bash. AdoptOpenJdk is deprecated and that is why I'm using Temurin. 1. Download Temurin using the mirrors or a package manager $ brew install --cask temurin And for other versions (like Java 8): $ brew tap homebrew/cask-versions $ brew install --cask temurin8 2. Add a bash script into your bash profile Open your ~/.zshrc or ~/.bash_profile (depending on what you're using it) And add this code: set-jdk() { jdkversion=$1 export JAVA_HOME=/Library/Java/JavaVirtualMachines/temurin-"$jdkversion".jdk/Contents/Home; export PATH=$PATH:$JAVA_HOME/bin; java -version } 3. Restart your terminal and change your Java version $ set-jdk N N being the Java version that you want and it's already installed e.g. $ set-jdk 8 To change to Java 8 / 1.8 Few considerations: It will only keep the Java version on the terminal session that you used the set-jdk command It won't set the Java version as global When you want to add the JAVA_HOME into another script/application, you can set /Library/Java/JavaVirtualMachines/temurin-<version>.jdk/Contents/Home (change the for the desired Java version. Like this JD-GUI example) Enjoy
As of 2023, brew install --cask zulu8 didn't work for me on Apple chip. brew install openjdk#8 also didn't not. The only one solution worked for me was brew install --cask adoptopenjdk8 and then of course vim ~/.zshrc and inside: export JAVAC_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/ export JAVA_11_HOME=$(/usr/libexec/java_home -v11) export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8) alias javac8='export JAVA_HOME=$JAVAC_HOME' alias java11='export JAVA_HOME=$JAVA_11_HOME' alias java8='export JAVA_HOME=$JAVA_8_HOME' switch from terminal using javac8 to a freshly installed (different) version. Note: you can have different Java versions than me, so it's just an example. Edit: for some reason it takes some time once it's activate. It seems to me a restart of a laptop is needed.
Here's a more DRY version for bash (Based on Vegard's answer) Replace 1.7 and 1.8 with whatever versions you are interested with and you'll get an alias called 'javaX'; where 'X' is the java version (7 / 8 in the snippet below) that will allow you to easily switch versions for version in 1.7 1.8; do v="${version: -1}" h=JAVA_"$v"_HOME export "$h"=$(/usr/libexec/java_home -v $version) alias "java$v"="export JAVA_HOME=\$$h" done
To install more recent versions of OpenJDK, I use this. Example for OpenJDK 14: brew info adoptopenjdk brew tap adoptopenjdk/openjdk brew cask install adoptopenjdk14 See https://github.com/AdoptOpenJDK/homebrew-openjdk for current info.
I followed steps in below link - https://medium.com/#euedofia/fix-default-java-version-on-maven-on-mac-os-x-156cf5930078 and it worked for me. cd /usr/local/Cellar/maven/3.5.4/bin/ nano mvn --Update JAVA_HOME -> "${JAVA_HOME:-$(/usr/libexec/java_home)}" mvn -version
New commands for installing Java via Homebrew: brew cask install adoptopenjdk/openjdk/adoptopenjdk8 brew cask install adoptopenjdk/openjdk/adoptopenjdk11 See the homebrew-openjdk repo for the latest commands. Installing Java You can install Java via Homebrew, Jabba, SDKMAN or manually. See this answer for details on all the commands. Switching Java versions* You can switch Java versions with jenv Jabba, SDKMAN or manually. See details on all the switching commands here. Best solutions Jabba is designed to work on multiple platforms, so it's a good option if you want a solution that'll also work on Windows Using Homebrew to download Java versions and jenv to switch versions provides a nice workflow. jenv makes it easy to work with Java versions stored in any directory on your machine, so it's a good alternative if you're interested in storing Java in non-default directories. Using SDKMAN to download Javas and switch versions is another great alternative Manually switching should be avoided because it's an unnecessary headache. Function to manually switch Java versions Here's the Bash / ZSH function for manually switching Java versions (by OpenJDK): jdk() { version=$1 export JAVA_HOME=$(/usr/libexec/java_home -v"$version"); java -version } There are great tools for switching Java versions, so I highly recommend against doing it manually.
Here is a great tutorial https://medium.com/#chamikakasun/how-to-manage-multiple-java-version-in-macos-e5421345f6d0 by using jEnv installed by brew or you can check out homebrew-openjdk https://github.com/AdoptOpenJDK/homebrew-openjdk managing version using brew Tap
To find the available Java versions: brew search java To install the latest stable version (19 as of today): brew info java brew install java For the system Java wrappers (eg: IDEs) to find the latest JDK, symlink it with: sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk To install a specific version (11): brew install java11 For the system, Java wrappers to find JDK 11: sudo ln -sfn /opt/homebrew/opt/openjdk#11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk You can have one of the OpenJDK versions in your PATH. Add one of the following lines to your ~/.bash_profile export PATH="/opt/homebrew/opt/openjdk/bin:$PATH export PATH="/opt/homebrew/opt/openjdk#11/bin:$PATH Then, source ~/.bash_profile
I solved this error on my mac m1 air just by adding the path of new jdk version in ~/.zshrc file. I have multiple jdks in my /Library/Java/JavaVirtualMachines path. So I can choose any version I wish to set as my default java path.