Thursday, July 12, 2018

How to install or update your preferred JDK with one single command on macOS or GNU/Linux?

What is your preferred JRE/JDK? Is it the Oracle JDK from or the JRE from, the OpenJDK from or the OpenJDK from or Whatever your preference is, you can install or update the JDK with only one single command on macOS or GNU/Linux with the bash script called update_jdk.

You can download it from here for free:

And here comes the one-liner:

sudo ./update_jdk -az -s $URL -t openjdk10 /opt/java/jdk/
The command above downloads the OpenJDK 10 from (you can specify the source and type with -s and -t), extracts it, applies the latest time zone data from IANA by downloading, extracting and calling the latest time zone updater (you have to accept the Oracle license by specifying the -a option) and it saves .version files that store the URI of the actual download for re-run purposes (see below). In /opt/java/jdk/ you will then find symlinks that point to the latest latest updated OpenJDK 10 folder (and the latest tzupdater.jar if you run the script with -z). Now you have a fixed path that you can use to configure your IDE, apps, whatever.
$ /opt/java/jdk/openjdk10_latest/bin/java -version
openjdk version "10.0.1-adoptopenjdk" 2018-04-17
OpenJDK Runtime Environment (build 10.0.1-adoptopenjdk+10)
OpenJDK 64-Bit Server VM (build 10.0.1-adoptopenjdk+10, mixed mode)
If you re-run the script above, it will only download the bits if they are different from those that you have downloaded already (it simply compares the .version file), updates the JDK that you have specified with -t and recreates the symlinks. So with the update_jdk script you stay up to date the easy way.

Update Aug 24, 2018:
bash-dwarfs has been renamed to bashberries

Update Sep 23, 2018:
update_jre has been renamed to update_jdk

Update Oct 13, 2019:
Option -s requires an exact URL that points to a JRE/JDK tarball, because it appears that the filenames on the servers are too volatile and future changes to them cannot be predicted.