Skip to content

Instantly share code, notes, and snippets.

@fcamblor
Last active November 9, 2020 13:49
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fcamblor/8a183ad77ff5c5a41d0c400e66231731 to your computer and use it in GitHub Desktop.
Save fcamblor/8a183ad77ff5c5a41d0c400e66231731 to your computer and use it in GitHub Desktop.
[MAC users] Updating Oracle JDK to OpenJDK

Starting from February 2019, Oracle JDK updates will require Commercial license if you make any business using Java (eg, you're a Java Developper not spending only your spare time developping with Java).

It includes Java not only on your server, but on your laptop as well.

Source | Source

What's nasty is Java versions will remains free to download since if you use it as an individual/personal use, you won't be concerned by the commercial aspects of the license.
Reading this article from Oracle changed my view on this statement : I don't think a "silent Java 8 update" will be possible with this important license change, which was my fear. However, you will still be able to download post-january Java 8 distributions on the Oracle website.. so I hope they will make it crystal clear that those new versions will be incompatible with a business activity.
Anyway, updating to OpenJDK builds and leave Oracle (Open)JDK behind remain a good thing to avoid this licensing mess.

Am I concerned ?

If you want to see if you're concerned, it is easy, simply run :

$> java -version
java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)

If the result contains Java HotSpot(TM) then you're using "Oracle JDK" or "Oracle Open JDK" which are both concerned by the license above and there are good chances you're going to need an update to OpenJDK.

Note that if you don't plan to update your current Oracle JDK versions to a post-january 2019 release, then you're safe regarding this commercial license constraint. I only feel it risky to do so as you may forget this constraint when you will upgrade your JDK in 6 monthes so ... not a good thing to back off to better blow up

But everything is not that straightforward for a regular Java Developper given that :

=> this is very likely that you will need both Java 8 and Java 11 (LTS) versions installed at the same time on your laptop to be able to both compile and run your new & legacy apps (personnally, I bet I'm going to need Java 8 for my legacy apps for a long long time, and will dedicate Java 11 to my newly-created apps only).

This document will help you to upgrade if you're a Mac User (I am a Mac user and tried to see the most common paths for this platform for the upgrade).

Linux & Windows users may find this guide useful though there will be differences mainly due to package managers (Windows users can rely on Chocolatey if they want a friendly package manager). But the idea will remain the same : try to install multiple OpenJDK versions at the same time, and easily switch between it through either jenv or alternatives.

Pre-requisites

This article describes how to install JDKs using Homebrew which is Mac-only. Some other cross-platform solutions exists : SDKMAN! and jabba allow to both install & switch between multiple versions of the JDK both for Mac / Linux / Cygwin(SDKMAN!)|Powershell(jabba) with the limitation of changing system-wide jdk for jabba.

First, make sure you installed :

  • Homebrew : see installation instructions on the Homebrew website (spoiler alert : you will need command line tool for XCode prior to install it)

  • Homebrew cask : once you have installed Homebrew, you will need access to cask formulae (=packages).
    Cask formulae are used for non-CLI packages (as soon as you need interactions outside the terminal, typically for applications).
    To install Cask, simply run :

    $> brew tap caskroom/cask
    
  • jenv : it is a useful utility aimed at managing/switching between multiple jdk versions on your system.
    To install it, you can follow installation instructions or simply run :

    $> brew install jenv
    # Replace `.bash_profile` by `.zshrc` if you're using zshrc, or any bootstrapping bash script either way
    # Those lines are important to have jenv being executed during your shell bootstrap
    $> echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
    $> echo 'eval "$(jenv init -)"' >> ~/.bash_profile
    

Installing latest OpenJDK 11 (LTS) version

If you only need to install OpenJDK 11, then it's super easy, simply run :

$> brew cask reinstall java

And that's it.

Note that by doing this, I'm not sure of the behaviour of cask when a new non-LTS Java (ex: Java 12) version will be released. I guess there will be some java-lts formulae which will be created soon or later to avoid auto-upgrades to non-LTS versions but it doesn't seem to be in place for now.

Personally, I will prefer to explicitely install targetted versions (see section below)

Installing targetted AdoptOpenJDK versions using Homebrew

If you need to install AdoptOpenJDK, then it's straightforward :

$> brew tap AdoptOpenJDK/openjdk
# List of available versions is available here : https://github.com/AdoptOpenJDK/homebrew-openjdk
$> brew cask install adoptopenjdk8
$> brew cask install adoptopenjdk11

On AdoptOpenJDK Website you will be able to download Windows and Linux distributions as well.

Also, I'm focusing on AdoptOpenJDK versions as this represents the largest distribution (multiple JDK version across multiple platforms) but other vendors are also providing their own (free) distributions such as Azul, IBM/Red Hat, Amazon or Bellsoft

Switching from JDK versions using jenv

First, declare instaled jdk versions into your jenv catalog :

# If you installed latest JDK with homebrew
$> jenv add /Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home/
# For adopt openjdk versions
$> jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/
$> jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.1.jdk/Contents/Home/

Then declare your system-wide used JDK :

# Showing available jdk versions added to the catalog
$> jenv versions
# Declaring global (system-wide, user specific) used jdk
$> jenv global 1.8

If you want to override system-wide JDK on a specific project, then :

$> cd /path/to/my/jdk11/new/project/
$> jenv local 11.0

This will create a /path/to/my/jdk11/new/project/.java-version file.

Behaviour of jenv is simply to redefine java* commands by looking at those .java-version files (in current dir and, if not found, in parent directories through the root dir ... then falling back on system-wide selected jdk when no .java-version file is found) and updating the good JAVA_HOME path.

Build tools (Maven, Gradle etc.) integration is available through jenv plugins, more on this on the jenv readme

Credits

I want to thank a lot :

@elecharny
Copy link

Great post !
A few suggestions:
o cask has to be installed
o it would be good to inform people about how to install homebrew, cask and jenv
o when installing a new Java version, you will have to provide the exact name when adding it to jenv. Typically, I just installed the latest Java 11 (adoptopenjdk-11.0.1.jdk) and running 'jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/' does not work, you have to run 'jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.1.jdk/Contents/Home/'

@fcamblor
Copy link
Author

fcamblor commented Jan 19, 2019

@elecharny thanks Emmanuel, I just updated the post with your feedback 👍

@marc0der
Copy link

marc0der commented Jan 20, 2019

Thanks for the great post!

Please also consider including SDKMAN! in your list of ways to install the JDK for the sake of completeness.

In case you haven't heard of it, it runs on OSX, Linux and Cygwin. It also allows you to install multiple JDK flavours and versions side-by-side so that you can switch between them at will. Since it installs everything in your home folder, it is not invasive to your system.

To date, we support the following JDKS:

  • OpenJDK
  • AdoptOpenJDK (coming soon!)
  • Amazon Coretto
  • Azul Zulu
  • Oracle JDK
  • GraalVM

We also cover versions going back as far as JDK 6.0 through to 13 Early Access (OpenJDK).

@davinkevin
Copy link

Another alternative I really like (instead of .jenv) is Jabba

For me, it supports fish shell which is a really great thing

@fcamblor
Copy link
Author

@marc0der @davinkevin thanks for pinpointing this, didn't heard of those tools before.

I just updated the article here and there

@fcamblor
Copy link
Author

Honestly, I would not recommend installing such distribution having that few reviews (github repository has only 16 starts, which means that very few people has been able to review its content)

We're talking about trust here, and personally (only my opinion here) I would only install a JVM widely trusted by the community, not a random distribution found here or there.

@bell-sw
Copy link

bell-sw commented Jan 23, 2019

First, we also would not trust some random OpenJDK builds. Here is some info that could help you decide.

Liberica JDK is a commercially supported OpenJDK distribution, backed by BellSoft bell-sw.com - the company is among Top-5 OpenJDK enterprise contributors according to https://blogs.oracle.com/java-platform-group/building-jdk-11-together.
BellSoft has a license to use TCK https://openjdk.java.net/groups/conformance/JckAccess/jck-access.html, which is Java SE Specification conformance suite. All Liberica JDK binaries are verified with it as well as other tests, and installers are signed. BellSoft engineers literally work on moving OpenJDK forward. One of JDK 11 features JEP-315 https://openjdk.java.net/jeps/315 which was integrated into OpenJDK 11 contributed by BellSoft/Cavium.
Finally, BellSoft was founded by former Oracle Java engineers who have been involved in the Java runtime development for more than a decade and you can meet us at FOSDEM and Jfocus in a couple weeks.

@fcamblor
Copy link
Author

Thanks for clarifications, I'm learning a lot in the JDK fields these days :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment