How to install Java with brew and jenv

jenv is a tool that helps you switch between different java versions in your development machine. But, Jenv doesn't install Java for you.

Use your platform appropriate package manager to install Java. On macOS, brew is recommended.

Install Java with Homebrew

First, ensure that you have Homebrew installed in your system. Homebrew is a package manager for Mac OS. So if you want to learn more details about it, visit my blog below.

I also posted how to install Java JDK manually in the following post.

Actually I already mentioned how to install Java JDK in the above post, but I will shortly show one more time

$ brew tap adoptopenjdk/openjdk
$ brew search jdk

==> Formulae
openjdk                                                                              openjdk@11

==> Casks
adoptopenjdk
adoptopenjdk12
adoptopenjdk13-openj9
adoptopenjdk14-openj9-jre-large
adoptopenjdk9
adoptopenjdk10
adoptopenjdk12-jre
adoptopenjdk13-openj9-jre
adoptopenjdk14-openj9-large
jdk-mission-control
adoptopenjdk11
adoptopenjdk12-openj9
adoptopenjdk13-openj9-jre-large
adoptopenjdk8
oracle-jdk
adoptopenjdk11-jre
adoptopenjdk12-openj9-jre
adoptopenjdk13-openj9-large
adoptopenjdk8-jre
oracle-jdk-javadoc
adoptopenjdk11-openj9
adoptopenjdk12-openj9-jre-large
adoptopenjdk14
adoptopenjdk8-openj9
sapmachine-jdk
adoptopenjdk11-openj9-jre
adoptopenjdk12-openj9-large
adoptopenjdk14-jre
adoptopenjdk8-openj9-jre
adoptopenjdk11-openj9-jre-large
adoptopenjdk13
adoptopenjdk14-openj9
adoptopenjdk8-openj9-jre-large
adoptopenjdk11-openj9-large
adoptopenjdk13-jre
adoptopenjdk14-openj9-jre
adoptopenjdk8-openj9-large
$ brew cask install adoptopenjdk
$ java --version

openjdk 14.0.1 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)

We need to add the below function in your ~/.bashrc or ~/.zshrc so that we can switch between different Java versions.

jdk() {
        version=$1
        export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
        java -version
 }

and run the following code to source

$ source ~/.zshrc

or

$ source ~/.bashrc

Let's install Java JDK 10

$ brew cask install adoptopenjdk10
$ jdk 10

That's all 🙂

Installing Jenv

Linux / OS X

$ git clone https://github.com/gcuisinier/jenv.git ~/.jenv

Mac OS X via Homebrew

$ brew install jenv

Bash

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

Zsh

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

Restart your shell by closing and reopening your terminal window.

To verify jenv was installed

$ jenv doctor

[OK]	JAVA_HOME variable probably set by jenv PROMPT
[OK]	Java binaries in path are jenv shims
[OK]	Jenv is correctly loaded

To make sure JAVA_HOME is set, make sure to enable the export plugin

$ jenv enable-plugin export
$ jenv enable-plugin maven

Adding Your Java Environment

I will install Java JDK from 8 to 14. They are installed in /Library/Java/JavaVirtualMachines/ folder

$ brew cask install adoptopenjdk8

==> Downloading https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u252-b09.1/OpenJDK8U-jdk_x64_mac_hotspot_8u252b09.pkg
Already downloaded: /Users/kenanhancer/Library/Caches/Homebrew/downloads/57a6643f7b6de270e63dfc545e0d665dc6de2c7081f7537ffba2ccdbdae46d2d--OpenJDK8U-jdk_x64_mac_hotspot_8u252b09.pkg
==> Verifying SHA-256 checksum for Cask 'adoptopenjdk8'.
==> Uninstalling Cask adoptopenjdk8
==> Uninstalling packages:
net.adoptopenjdk.8.jdk
==> Purging files for version 8,252:b09.1 of Cask adoptopenjdk8
==> Installing Cask adoptopenjdk8
==> Running installer for adoptopenjdk8; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
installer: Package name is AdoptOpenJDK
installer: Upgrading at base path /
installer: The upgrade was successful.
package-id: net.adoptopenjdk.8.jdk
version: 1.8.0_252-b09
volume: /
location: Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk
install-time: 1589071635
🍺  adoptopenjdk8 was successfully installed!
$ brew cask install adoptopenjdk9
$ brew cask install adoptopenjdk10
$ brew cask install adoptopenjdk11
$ brew cask install adoptopenjdk12
$ brew cask install adoptopenjdk13
$ brew cask install adoptopenjdk14

Adding installed Java JDK paths to jenv

$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-9.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-10.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-12.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home

Listing Java JDKs

$ jenv versions

  system
* 1.8 (set by /Users/kenanhancer/.jenv/version)
  1.8.0.252
  10.0
  10.0.2
  11.0
  11.0.7
  12.0
  12.0.2
  13.0
  13.0.2
  14.0
  14.0.1
  9
  openjdk64-1.8.0.252
  openjdk64-10.0.2
  openjdk64-11.0.7
  openjdk64-12.0.2
  openjdk64-13.0.2
  openjdk64-14.0.1
  openjdk64-9

Setting a Global Java Version

$ jenv global 10

Setting a local Java Version(per directory)

$ jenv local 11

Setting a shell Java Version

$ jenv shell 12

Uninstalling adoptopenjdk/openjdk and jenv and from Homebrew

$ brew untap adoptopenjdk/openjdk
$ brew cask uninstall $(brew list --cask | grep "^adopt")
$ brew uninstall jenv
$ rm ~/.jenv

2 thoughts on “How to install Java with brew and jenv

  • thecesrom says:

    Great guide!

    But there is a warning when I run brew cask ls -1:
    Warning: Calling brew cask list is deprecated! Use brew list –cask instead.

    I did this instead:
    brew cask uninstall $(brew list –cask | grep "^adopt")

Leave a Reply

Your email address will not be published. Required fields are marked *