Skip to content

Instantly share code, notes, and snippets.

@svkrclg
Forked from gramcha/having-multiple-jdk-macos.md
Created September 12, 2024 18:09
Show Gist options
  • Save svkrclg/b93e1dc458746ccfeaab6e4a339aa250 to your computer and use it in GitHub Desktop.
Save svkrclg/b93e1dc458746ccfeaab6e4a339aa250 to your computer and use it in GitHub Desktop.
Managing multiple Java versions in MacOS

Installing different versions of open jdk through Homebrew(assuming already installed) and already having Java 8.

We need to install a tool called jenv - Java version manager which is similar to nvm(nodeJs version manager).

brew install jenv

Export the jenv path to .bash_profile or .zshrc - whatever you are using. I am using .zshrc

echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc

After executing above command make sure you are having the below in ~/.zshrc

export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

Then update the terminal using

source ~/.zshrc

Check the jenv health

jenv doctor

It will return something similar to this

[OK]	No JAVA_HOME set
[ERROR]	Java binary in path is not in the jenv shims.
[ERROR]	Please check your path, or try using /path/to/java/home is not a valid path to java installation.
	PATH : /usr/local/Cellar/jenv/0.5.2/libexec/libexec:/Users/gramcha/.jenv/shims:/Users/gramcha/.jenv/shims:/Users/gramcha/.jenv/bin:/Users/gramcha/.jenv/bin:/Users/gramcha/chromedriver:/opt/local/bin:/opt/local/sbin:/Users/gramcha/.jenv/shims:/Users/gramcha/.jenv/bin:/Users/gramcha/.jenv/bin:/Users/gramcha/chromedriver:/opt/local/bin:/opt/local/sbin:/Users/gramcha/.jenv/shims:/Users/gramcha/.jenv/bin:/Users/gramcha/.jenv/shims:/Users/gramcha/.jenv/bin:/Users/gramcha/chromedriver:/opt/local/bin:/opt/local/sbin:/Users/gramcha/.nvm/versions/node/v10.17.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
[OK]	Jenv is correctly loaded

No need to worry about error message as long as Jenv is correctly loaded is printed.

To ensure that JAVA_HOME is correct

jenv enable-plugin export

Assuming Maven is used and to educate maven about Java version used

jenv enable-plugin maven

Intalling latest open jdk version(15) at the time of writing this script

brew install openjdk

It will printout

Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/openjdk-15.0.1.mojave.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/a4f00dc8b4c69bff53828f32c82b0a6be41b23a69a7775a95cdbc9e01d9bdb68?response-content-disposition=attachment%3Bfilename%3D%22openjdk-15.0.1.mojave.bottle.tar.gz%22&Policy=eyJTdGF0ZW
######################################################################## 100.0%
==> Pouring openjdk-15.0.1.mojave.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 it shadows the macOS `java` wrapper.

If you need to have openjdk first in your PATH run:
  echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk/include"

==> Summary
🍺  /usr/local/Cellar/openjdk/15.0.1: 614 files, 323.8MB

Execute the caveats suggested by above install

sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc
export CPPFLAGS="-I/usr/local/opt/openjdk/include"

similarly if you want to install java8 execute below and follow the caveats.

brew install openjdk@8

I already having AdoptOpenJDK 8 in my machine. In case if you want to install that

brew install AdoptOpenJDK/openjdk/adoptopenjdk{8,11}

Execute below command to see available JVM's in machine

/usr/libexec/java_home -V

It will print something like this

Matching Java Virtual Machines (2):
    15.0.1, x86_64:	"OpenJDK 15.0.1"	/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
    1.8.0_212, x86_64:	"AdoptOpenJDK 8"	/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home

Add java 15 and java 8 paths to jenv by copying path from above previous command output.

jenv add /Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

It will print the below outputs respectively.

openjdk64-15.0.1 added
15.0.1 added
15.0 added


openjdk64-1.8.0.212 added
1.8.0.212 added
1.8 added

Check the jenv for available versions

jenv versions

It will print below outputs

* system (set by /Users/gramcha/.jenv/version)
  1.8
  1.8.0.212
  15.0
  15.0.1
  openjdk64-1.8.0.212
  openjdk64-15.0.1

Set system level version

jenv global 15.0

and check the java version

java -version

and it should print 15.0

openjdk version "15.0.1" 2020-10-20
OpenJDK Runtime Environment (build 15.0.1+9)
OpenJDK 64-Bit Server VM (build 15.0.1+9, mixed mode, sharing)

Some of projects wants older version like java8 to support that add project wise version. Go inside the specific project folder and execute below command

jenv local 1.8

This will create the .java-version file that describes which java version to use.

To set the java specific version at teminal level execute that

jenv shell 1.8

Check the jenv versions it should print

system
* 1.8 (set by JENV_VERSION environment variable)
  1.8.0.212
  15.0
  15.0.1
  openjdk64-1.8.0.212
  openjdk64-15.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment