How to Install OpenJDK | Oracle Java on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to install OpenJDK and Oracle Java on Ubuntu 20.04 | 18.04.

For anyone building Java based applications, they’re going to be using the Java programming language. Java language is popular and in almost all operating systems.

Java can be implemented in multiple ways. When it comes to Linux, there are two primary Java implementations.

OpenJDK and Oracle Java. There two are identical, except for Oracle Java, which has some few commercial features and comes with some restrictions on how to use the software.

Oracle Java license limits Java for personal and development usage only. You cannot use it for commercial purposes.

OpenJDK is a free, opensource Java software which allows anyone from anywhere to freely use as they see fit.

OpenJDK consists of Java Runtime Environment (JRE) and Java Development Kit (JDK).

JRE consists of the Java virtual machine (JVM), classes, and binaries that allow you to run Java programs. JDK includes JRE and development/debugging tools and libraries necessary to build Java applications.

When you’re ready to install both Java software, use either methods below:

Install OpenJDK on Ubuntu

At the time of this writing, Java 11 is the latest LTS version of Java and it’s also the default Java version in Ubuntu 20.04.

To install the latest version of OpenJDK in Ubuntu, run the commands below:

sudo apt update
sudo apt install openjdk-11-jdk

To verify and validate that Java is installed, run the commands below:

java -version

That should display similar lines as show below:

penjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

That’s how to install Java. The commands above install the full Java packages, including JDK, JRE and all libraries.

If you only want to install Java JRE instead of the whole suite, then simply run the commands below:

sudo apt update
sudo apt install openjdk-11-jre

Or if you just want the bare minimal of Java Runtime, simply run the commands below:

sudo apt update
sudo apt install openjdk-11-jdk-headless

Some applications still don’t fully support the latest OpenJDK 11. For those, they can install the previous Java LTS which was version 8.

sudo apt update
sudo apt install openjdk-8-jdk

If you have multiple versions of Java installed, simply use the commands below to set which one should be the default for your system.

sudo update-alternatives --config java

That should then be able to select which version is the default.

ere are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

Press  to keep the current choice[*], or type selection number: 

Some program require that JAVA_HOME is configured on the system. You can set the default home by using the lines above in the config file.

For Java 11, it displays /usr/lib/jvm/java-11-openjdk-amd64 and Java 8, it’s /usr/lib/jvm/java-8-openjdk-amd64.

To set their homes, run the commands below to open the system environment file.

sudo nano /etc/environment

Then add a line for Java 11 as below:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Run the commands below to save your changes.

source /etc/environment

That should do it!

If you want to install Oracle Java on Ubuntu, then use the method below:

Install Oracle Java on Ubuntu

At the time of this writing, the latest Oracle Java version is 14.

Download the latest Java SE Development Kit 14 LTS release from it from the link below.

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Take notes of the version number you’re downloading… If there’s a newer version number then the one below, select it instead.

You can also easily install Java DEB package by running the commands below… At the time of this writing, the current latest version of Java JDK is jdk-14

You may have to replace the version number with the latest as it becomes available..

cd /tmp
wget --no-check-certificate -c --header  "Cookie: oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn-pub/java/jdk/14+36/076bab302c7b4508975440c56f6cc26a/jdk-14_linux-x64_bin.deb"

Now that you’ve downloaded the correct archive package for your system, run the commands below to install Oracle Java…

Again, the current latest version is jdk-14

sudo apt install ./jdk-14_linux-x64_bin.deb

After installing, you should get a message similar to the one below:

The following NEW packages will be installed:
  jdk-14
0 upgraded, 1 newly installed, 0 to remove and 4 not upgraded.
Need to get 0 B/166 MB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 /tmp/jdk-14_linux-x64_bin.deb jdk-14 amd64 14-1 [166 MB]
Selecting previously unselected package jdk-14.
(Reading database ... 220315 files and directories currently installed.)
Preparing to unpack /tmp/jdk-14_linux-x64_bin.deb ...
Unpacking jdk-14 (14-1) ...
Setting up jdk-14 (14-1) ...

After that, run the commands below to configure Java 14 as the default on Ubuntu… The commands below configure Ubuntu to use Java alternatives….

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-14/bin/java 3
sudo update-alternatives --config java

If you have other versions of Java installed and you ran the commands above, you should see a prompt to select the version of Java you want to make the default.

If you don’t have another versions of Java installed, then the commands will return nothing…

Next, run the commands below to make Java 14 to be the default Java compiler for your Ubuntu desktop…

sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk-14/bin/jar 3
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-14/bin/javac 3
sudo update-alternatives --set jar /usr/lib/jvm/jdk-14/bin/jar
sudo update-alternatives --set javac /usr/lib/jvm/jdk-14/bin/javac

That should get Java installed and configured…..

Run the commands below see if Ubuntu recognizes Java..

java -version

You should see the output below:

Output:
java version "14" 2020-03-17
Java(TM) SE Runtime Environment (build 14+36-1461)
Java HotSpot(TM) 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)

To set JAVA environment variables, create a new file in the /etc/profile.d directory for Java JDK…

sudo nano /etc/profile.d/jdk14.sh

Then copy and paste the lines into the end of the file and save…

export J2SDKDIR=/usr/lib/jvm/jdk-14
export J2REDIR=/usr/lib/jvm/jdk-14
export PATH=$PATH:/usr/lib/jvm/jdk-14/bin:/usr/lib/jvm/jdk-14/db/bin
export JAVA_HOME=/usr/lib/jvm/jdk-14
export DERBY_HOME=/usr/lib/jvm/jdk-14/db

Next, run the commands below

source /etc/profile.d/jdk14.sh

The commands above should configure Java to work and function with Ubuntu. To test if Java is installed correctly, run the commands below.

You can now verify that Java is installed and configured by outputting its home directory.

echo $JAVA_HOME

It should output similar line as below:

Output:
/usr/lib/jvm/jdk-14

Conclusion:

This post showed you how to install both OpenJDK and Oracle Java on Ubuntu 20.04 | 18.04. If you find any error above, please use the comment form below to report.

Thanks,

You may also like the post below:

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.