Installing OpenJDK in 2020

Installing OpenJDK in 2020

Introduction

<Skip Intro>

A few years back, when we want to download a JDK installer, we go to the Oracle Technology Network website, and download the JDK from it.

Nowadays, you can still download the latest bleeding-edge JDK version (currently is 13.0.2) in the Oracle website after accepting their license terms. If you need to download older JDK versions such as 8 or 11, you need an Oracle account.

oracle-login
Login page that appears when you try to download JDK 8 or 11 from Oracle

Why do we need older Java versions such as 8 and 11? Java 8 and 11 are long-term-support (LTS) versions. These versions are supported and maintained for more than 5 years, and non-LTS versions are only supported for 6 months and their download links are removed from the site after their support has ended. Since LTS are more stable, big projects are made with it. New developers joining their development team needs to download a compatible JDK for themselves. Examples of big projects that uses older JDK are Android Studio and Kotlin Language. According to a study: 64% of developers report that Java 8 remains the most often used release.

OpenJDK as an alternative

OpenJDK is advertised by Oracle itself and other seasoned Java developers. In fact 36% of developers switched from Oracle JDK to an alternate OpenJDK distribution, over the last year 2019.

Installing OpenJDK

Option 1: AdoptOpenJDK Website

AdoptOpenJDK is a community of Java users that distributes a pre-compiled JDK distribution for public download.

To install a JDK, go to their website https://adoptopenjdk.net/ and choose the version you want to download. The website automatically detects your OS and CPU architecture – so no need to bother about that.

adoptopenjdk-home
AdoptOpenJDK hompage with download button upfront

If you need to download other releases, just go to their releases page (https://adoptopenjdk.net/releases.html) and choose the correct release that you need.

After downloading, just execute the installer file – which is usually double-clicked in most OS.

Option 2: SDKman tool

SDKman is a command-line tool that manages your JDK installation and other JVM related tools such as Gradle, Maven, Grails, Spring Boot, Kotlin, etc...

To install SDKman, just download the bash script from their site. (docs)

curl -s "https://get.sdkman.io" | bash
Downloads and installs SDKman using the installer script. Might take a while, depending on internet download speed.

When the download completes, run this source command or start a new terminal session.

source "$HOME/.sdkman/bin/sdkman-init.sh"
This source command starts the SDKman installation in the current shell session. Opening a new shell session works, too.

When the installation is complete, you can now use the sdk commands to install JDK.

First, you must determine the JDK version you need to install:

sdk list java
Show all JDK versions available for download.
================================================================================
Available Java Versions
================================================================================
 Vendor        | Use | Version      | Dist    | Status     | Identifier
--------------------------------------------------------------------------------
 AdoptOpenJDK  |     | 13.0.2.j9    | adpt    | installed  | 13.0.2.j9-adpt      
               |     | 13.0.2.hs    | adpt    |            | 13.0.2.hs-adpt      
               |     | 12.0.2.j9    | adpt    |            | 12.0.2.j9-adpt      
               |     | 12.0.2.hs    | adpt    |            | 12.0.2.hs-adpt      
               |     | 11.0.6.j9    | adpt    |            | 11.0.6.j9-adpt      
               |     | 11.0.6.hs    | adpt    |            | 11.0.6.hs-adpt      
               |     | 8.0.242.j9   | adpt    |            | 8.0.242.j9-adpt     
               |     | 8.0.242.hs   | adpt    |            | 8.0.242.hs-adpt     
 Amazon        |     | 11.0.6       | amzn    |            | 11.0.6-amzn         
               |     | 8.0.242      | amzn    |            | 8.0.242-amzn        
 ## truncated dozens more choices
Example sdk list java output

From the list of available versions, install the version you need by running the sdk install java <Identifier> command.

sdk install java 8.0.242.hs-adpt
Example install command that install JDK 8 Hotspot edition from AdoptOpenJDK.

When the command finishes, the chosen JDK version is now installed and configured. You can now use java or javac command in the terminal to build Java programs.

In case you need to install multiple versions of Java, just run the sdk install java ... command again. After the installation, you need to set the installed Java version to be used in the current shell session temporarily or permanently.

sdk current java <Identifier>
Sets the <Identifier> as the active Java version in the current shell session.
sdk default java <Identifier>
Sets the <Identifier> as the active Java version in the current shell session and the future shell sessions.

Conclusion

In Linux or MacOS, prefer to install using SDKman. The CLI tool SDKman provides simplifies the download, install, and configuration process of a JDK. In Windows, SDKman is not natively supported. If you want to insist on using SDKman on Windows, you might need to do it in WSL, or Cygwin, or Git Bash.

When using Windows, download an *.msi installer file from https://adoptopenjdk.net/, then run it just like any executable file in Windows.

Show Comments