Install OpenJDK 11 on macOS

Install using HomeBrew

$ brew install --cask adoptopenjdk/openjdk/adoptopenjdk11
...
==> Installing Cask adoptopenjdk11
==> Running installer for adoptopenjdk11; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
Password:

Manual Install from AdoptOpenJDK

See: https://adoptopenjdk.net/installation.html

Manual Install from jdk.java.net

Update download url based on the latest version available in this download page: jdk.java.net/archive.

$ cd /tmp
$ curl -O https://download.java.net/java/GA/jdk11/9/GPL/openjdk-11.0.2_osx-x64_bin.tar.gz
$ tar xf openjdk-11.0.2_osx-x64_bin.tar.gz
$ sudo mv jdk-11.0.2.jdk /Library/Java/JavaVirtualMachines/

Listing Installed JDK Version

$ /usr/libexec/java_home -h
Usage: java_home [options...]
    Returns the path to a Java home directory from the current user's settings.

Options:
    [-v/--version   <version>]       Filter versions (as if JAVA_VERSION had been set in the environment).
    [-a/--arch      <architecture>]  Filter architecture (as if JAVA_ARCH had been set in the environment).
    [-F/--failfast]                  Fail when filters return no JVMs, do not continue with default.
    [   --exec      <command> ...]   Execute the $JAVA_HOME/bin/<command> with the remaining arguments.
    [-X/--xml]                       Print full JVM list and additional data as XML plist.
    [-V/--verbose]                   Print full JVM list with architectures.
    [-h/--help]                      This usage information.
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
    11.0.9.1 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
    11.0.2 (x86_64) "Oracle Corporation" - "OpenJDK 11.0.2" /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
    1.8.0_212 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

Switching JDK Version

Get JAVA_HOME path and set it to environment variable.

Add the following to ~/.bashrc , ~/.zshrc etc.

export JAVA_HOME=`/usr/libexec/java_home -V 2>&1 | grep 11.0.9 | grep -o '/Library/.*'`
PATH=$JAVA_HOME/bin:$PATH

$ /usr/libexec/java_home -V 2>&1 | grep 11.0.9 | grep -o '/Library/.*'
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

$ export JAVA_HOME=`/usr/libexec/java_home -V 2>&1 | grep 11.0.9 | grep -o '/Library/.*'`
$ PATH=$JAVA_HOME/bin:$PATH

$ java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)

JShell

Also see: https://docs.oracle.com/en/java/javase/11/jshell/snippets.html

$ jshell
|  Welcome to JShell -- Version 11.0.9.1
|  For an introduction type: /help intro

jshell> /help intro
|
|                                   intro
|                                   =====
|
|  The jshell tool allows you to execute Java code, getting immediate results.
|  You can enter a Java definition (variable, method, class, etc), like:  int x = 8
|  or a Java expression, like:  x + x
|  or a Java statement or import.
|  These little chunks of Java code are called 'snippets'.
|
|  There are also the jshell tool commands that allow you to understand and
|  control what you are doing, like:  /list
|
|  For a list of commands: /help

jshell> /exit
|  Goodbye

Leave a Comment

Your email address will not be published.

twelve − 6 =