Installing all the Javas on a Mac and switching between them
Using Brew install all the major version of Java
brew install openjdk@8
brew install openjdk@11
brew install openjdk@17
brew install openjdk@21
brew install openjdk@25
Then install jEnv to manage them
brew install jenv
After the install has completed add this to your ~/.bash_profile and restart the shell.
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"
jenv enable-plugin export
Add the paths to the JAVA_HOMEs. I've just shown the output from one of them so you can see.
$ jenv add /usr/local/opt/openjdk@8
$ jenv add /usr/local/opt/openjdk@11
openjdk64-11.0.29 added
11.0.29 added
11.0 added
11 added
$ jenv add /usr/local/opt/openjdk@17
$ jenv add /usr/local/opt/openjdk@21
$ jenv add /usr/local/opt/openjdk@25
Next check then it thinks we are looking at
$ jenv version
system (set by /Users/mrn/.jenv/version)
Nothing! Let's see what we have actually added:
$ jenv versions
* system (set by /Users/mrn/.jenv/version)
1.8
1.8.0.472
11
11.0
11.0.29
17
17.0
17.0.17
21
21.0
21.0.9
25
25.0
25.0.1
openjdk64-1.8.0.472
openjdk64-11.0.29
openjdk64-17.0.17
openjdk64-21.0.9
openjdk64-25.0.1
Great I think I'll have some Java 11 please:
$ jenv global 11
$ echo $JAVA_HOME
/Users/mrn/.jenv/versions/11
$ java -version
openjdk version "11.0.29" 2025-10-21
OpenJDK Runtime Environment Homebrew (build 11.0.29+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.29+0, mixed mode)
Changed my mind, I want Java 21:
$ jenv global 21
$ echo $JAVA_HOME
/Users/mrn/.jenv/versions/21
$ java -version
openjdk version "21.0.9" 2025-10-21
OpenJDK Runtime Environment Homebrew (build 21.0.9)
OpenJDK 64-Bit Server VM Homebrew (build 21.0.9, mixed mode, sharing)
All without having to restart the shell!
The more observant of you will notice I used the keyword global. This sets to the default version. You can also use the keyword local which fixes the java version for this directory and below. This is great for when you have different Java projects all using different versions of Java or you need to easily switch java version when compatibility testing.
No feedback yet
Form is loading...