Install JDK Ubuntu

JVM (Virtual Machine of Java) and Java are needed for various types of software including Jenkins, Cassandra, Glassfish, Jetty, and Tomcat.

In this article, we will install many releases of Java Development Kit (JDK) and Java Runtime Environment (JRE) using 'apt'. We will install OpenJDK and the official JDK as well from Oracle. We will then choose the release we wish to use for our projects. When we are finished, we will be able to apply the JDK for developing software or using the Java Runtime for running software.

The repositories of Ubuntu 20.04 (default) contain two packages of OpenJDK, Java Development Kit, and Java Runtime Environment. The Java Runtime Environment is composed of the Java Virtual Machine, binaries, and classes that permit us to execute Java programs. The Java Development Kit contains the Java Runtime Environment and debugging/development tools as well as libraries mandatory for building Java applications.

  • Java Development Kit is a Java Technology distribution by Oracle Corporation.
  • JDK implements JLS (Java Language Specification) and JVMS (Java Virtual Machine Specification) and offers SE (Standard Edition) of the API (Java Application Programming Interface).
  • It offers software to work with Java applications.
  • Some examples of added software are a compiler, a virtual machine, a debugger, performance monitoring tools, and other services that Oracle considers helpful for Java programmers.
  • Besides, Oracle has published the latest software version upon the NFTC (Oracle No-Free Terms and Conditions) license.

Oracle publish binaries for x86-64 architecture for Linux, macOS, and Windows-based OSes and for the aarch64 architecture for Linux and macOS. Older versions supported the SPARC architecture and Oracle Solaris OS. The primary implementation of Oracle of the JVMS is called the HotSpot.

JDK contents

JDK contains a group of programming tools as its main components, including:

  • apt: It stands for annotation processing tool.
  • appletviewer: It can be used to execute and debug several Java applets without any web browser.
  • extcheck: It is a utility that finds the conflicts of a JAR file.
  • idlj: This utility stands for the IDL-to-Java compiler. It produces Java bindings using a provided Java IDL file.
  • java: For Java Applications, it is a loader and interpreter that interprets many class files produced by the javac compiler. A single launcher is not utilized for both deployment and development. jre, the previous deployment launcher, is no longer provided in Sun JDK. Rather it has been substituted by this java loader.
  • jabswitch: It is the Java Access Bridge. It is used for exposing assistive automation in Microsoft Windows operating systems.
  • javac: It is a Java compiler. It transforms source code into the Java bytecode.
  • jar: It is an archiver used to package corresponding class libraries into one JAR file. Also, this tool helps in managing JAR files.
  • javadoc: It is a document generator which produces documentation using the comments of source code automatically.
  • javafxpackager: It is a tool used for packaging and signing JavaFX applications.
  • javah: It is a stub generator and C header, which is used for writing native methods.
  • jarsigner: It is a tool of jar sign and verification.
  • javap: It is the class file disassembler.
  • JConsole: Java Management and Monitoring Console.
  • javaws: For JNLP applications, it is a Java Web Start launcher.
  • jdb: It is a debugger.
  • jinfo: It brings configuration details from an active crash dump or Java process.
  • jhat: It is a Java Heap Analysis Tool.
  • jmap Oracle jmap: For memory, it results in the memory map can print distributed heap memory and object memory map details of a provided core dump or process.
  • jmc: It is a command-line script shell of Java.
  • jshell: It is a real-eval-print loop specified in Java 9.
  • jstack: This utility can print Java stack traces.
  • jstat: It is the statistics monitoring tool of Java Virtual Machine.
  • jstatd: It is the jstat daemon.
  • keytool: It is a tool to manipulate the keystore.
  • policytool: It is a management and policy creation tool which can decide a policy for Java runtime, defining which permissions are for code from several sources.
  • pack200: It is a JAR compression tool.
  • VisualVM: It is a visual tool which is used to integrate many JDK tools of the command line, memory profiling abilities, and lightweight performance.
  • xjc: It is the Java API part for XML Binding API. It approved an XML schema and produced Java classes.
  • wsimport: It produces portable artifacts of JAX-WS to invoke a web service.

Experimental tools might not be in the future releases of the JDK. Also, the JDK provides a complete JRE (Java Runtime Environment), usually known as private runtime, because of the fact that it's isolated from the "Regular" JRE and includes additional content.

It is composed of a Java Virtual Machine; each class library is available inside the production environment, and extra libraries are only helpful for developers, including the IDL libraries and the internationalization libraries.
Also, the JDK copies contain several example selections showing the use of most of the Java API portion.

Other JDKs

Many other JDKs are usually available for a range of platforms along with the mostly used JDK, a few of which were initiated from the Sun JDK source and a few that didn't. Each adheres to the common Java specifications but differs in unspecified areas explicitly, including optimization techniques, compilation strategies, and garbage collection.

They contain:

In maintenance or development mode:

  • z/OS, Pocket PC, OS/400, MVS, Windows, Linux, fox AIX, IBM J9 JDK
  • Aicas JamaicaVM
  • IcedTea/OpenJDK
  • OpenJDK-based Zulu/Azul Systems for Linux, Mac OS X, Windows, cloud, and the embedded
  • Low latency Linux JDK, Azul Systems Zing

Discontinued or not being managed:

  • JRockit JDK of Oracle Corporation for Solaris, Linux, and Windows
  • GCJ and Classpath of GNU
  • Blackdown Java: JDK port of Sun for Linux
  • Mac OS Runtime for Java JDK/JVM of Apple for traditional Mac OS
    Apache Harmony

Releases of JDK

JDK 10

JDK 10 is an open-source Java SE 10 Platform's reference implementation specified by JSR 383 within the Java Community Process. On 20 March 2018, JDK 10 accomplished General Availability. Production-ready binaries upon the GPL are from Oracle. The schedule and features of this version were introduced and tracked by the JEP Process, as modified by the JEP 2.0 proposal.

JDK 11

It was published on 25 September 2018, and this release is accessible for bug fixes currently. It provides Long-Term Support or LTS. Java 11 contains several new aspects, some of which are as follows:

  • JEP 320: Remove the CORBA and Java EE Modules
  • JEP 318: A No-Op Garbage Collector: Epsilon
  • JEP 315: Developed Aarch64 Intrinsics
  • JEP 309: Class-File Constants
  • JEP 181: Nest-based Access Control

Several aspects from older versions were discarded; in particular, Java Web Start and Java Applets are no longer present. CORBA, Java EE, and JavaFX modules have been deleted from JDK.

JDK 12

It was published on 19 March 2019. Java 12 contains several new aspects, including:

  • JEP 334: JVM Constants API
  • JEP 325: Switch Expressions
  • JEP 230: Microbenchmark Suite

The JEP 325 preview feature expands the switch statement; hence, it can be utilised as an expression and includes a new style of case label in which an expression is on the right-hand side. The break statement is not required. A yield statement can be utilized for complex expressions.

JDK 13

It was published on 17 September 2019. Java 13 contains the below new aspects and several bug fixes and smaller enhancements:

  • JEP 355: Text Blocks
  • JEP 354: Switch Expressions
  • JEP 353: Reimplement Legacy Socket API
  • JEP 351: Unused Uncommit Memory: ZGC
  • JEP 350: CDS Archives

JDK 14

JDK 14 was published on 17 March 2020. It contains the below new aspects and several bug fixes and smaller enhancements:

  • JEP 352: Non-Volatile Mapped Bute Buffers
  • JEP 349: JFR Event Streaming
  • JEP 345: G1 NUMA-Aware Memory Allocation
  • JEP 343: Packaging Tool
  • JEP 305: instanceof Pattern Matching

JDK 15

JDK 15 was published on 15 September 2020. It supports Text Blocks (multi-line string literals). The Z and Shenandoah garbage collectors are ready to utilize in production. Support for the Solaris operating system of Oracle is abandoned. The Nashorn JavaScript Engine is discarded. Also discarded a few root CA certificates.

JDK 16

JDK 16 was published on 16 March 2021. It drops Ahead-of-Time compilation aspects. The Java implementation is still and was itself specified in the C++ language, while the latest C++14 is permitted as of Java 16. Also, the code was moved to GitHub, discarding Mercurial as a source control system.

JDK 17

JDK 17 is the modern LTS version since September 2021. This release is the second LTS version since shifting to the new six-month version cadence. JEP 406 develops the syntax of pattern matching utilized in instanceof tasks to switch expressions and statements. It permits cases to be chosen based on the category of argument, refining patterns, and null cases.

JDK 18

JDK 18 was published on 22 March 2022. Some new aspects are as follows:

  • JEP 413: Code Snippets within the Java API Documentation
  • JEP 408: Web Server
  • JEP 400: By default, UTF-8

JDK 19

JDK 19 was published on 20 September 2022. Some new aspects are as follows:

  • JEP 424: Memory API & Foreign Function
  • JEP 422: RISC-V/Linux Port
  • JEP 405: Record Patterns

JEP 405 permits record patterns, expanding the capabilities of pattern matching aspect of switch expressions and instanceof operators to add record patterns that refer to the elements of the record explicitly.

JDK 20

JDK 20 was published on 21 March 2023. Some new aspects are as follows:

  • JEP 433: Switch Pattern Matching
  • JEP 432: Record Patterns
  • JEP 429: Scoped Values

Prerequisites

We will need to follow this article:

The 20.04 server set up of Ubuntu by following the 20.04 initial server setup guide of Ubuntu including a non-root sudo user and a firewall.

Install the default JDK/JRE

One of the easiest options to install Java is to apply the version which is packaged using Ubuntu. The 20.04 version of Ubuntu by default includes the 11 version of OpenJDK and it is an open-source version of the JDK and JRE.

First, we need to update the index of the package for installing this version.


Install JDK Ubuntu

Next, see if the Java version is installed already.


Install JDK Ubuntu

We will see the following result if the Java version is not installed currently.

We need to run the below command for installing the default Java Runtime Environment which will install the JRE using OpenJDK 11.


Install JDK Ubuntu

The Java Runtime Environment will permit us to execute almost every Java software.

We can verify our installation using the following command.

We will find results similar to the below:

Install JDK Ubuntu

We may require the Java Development Kit in addition to the Java Runtime Environment for compiling and running some particular Java-based software. We need to run the below command for installing the JDK which will install the JRE as well.


Install JDK Ubuntu

We need to verify the JDK is successfully installed by checking the Java Compiler javac version.


Install JDK Ubuntu

We will see e the below result:

Next, let's see how to install the official JDK and JRE of Oracle.

Install Oracle JDK 11

The licensing agreement of Oracle for Java does not permit automatic installation from the package manager. We should create an account of Oracle and download the JDK manually for adding any new package repository for the release we had like to use for installing the Oracle Java Development Kit which is the official release shared by Oracle. Then, we can apply apt for installing it with the help of the third-party installation script.

We will need to install must match the release of the installer script of the JDK version of Oracle. We should visit a page, i.e., 'oracle-java11-installer' for finding out which release we need.

The script version is 11.0.7 on this page. We would require the 11.0.7 version of Oracle JDK in this case. Our version number might vary depending on when we are installing the software. We do not need to install anything using this page, we will download the script of the installation from apt shortly.

Then, we need to check the downloads page and after that locate the release that matches the one we need.

  • Click on the button, i.e., 'JDK Download' and we will be jumped to a screen that displays the releases available.
  • Click on the package, i.e., '.tar.gz' for Linux.
    We will be illustrated with the screen prompting us for accepting the license agreement of Oracle.
  • Choose the checkbox for accepting this license agreement and click on the Download button.
  • Our download will start.
  • We might require to login into our Oracle account multiple times before the download begins.
  • We will need to send it to our server when the file has been downloaded. We will upload this file to our server on our local machine.
  • On Windows, Linux, or, macOS with the help of Windows Subsystem for Linux, we will apply the command, i.e., 'scp' for transferring the file to our home directory of Sammy user.
  • The below command assumes that we have saved the JDK file of Oracle to the Downloads folder of our local machine.

We will return to our server and add a repository of the third party that will support us to install the Java of Oracle when the file uploading process has been completed. We will install a package, i.e., 'software-properties-common' which will add a command, i.e., 'add-apt-repository' to our system.


Install JDK Ubuntu

Next, we will import a signing key which is used for verifying the software we are about to install:


Install JDK Ubuntu

We will see the following output:

Then, we will use a command, i.e., 'add-apt-repository' for adding the repository to our list of the package sources:


Install JDK Ubuntu

We will see the following message:

We need to press the enter button for continuing the installation process. We might find a message about 'no valid OpenPGP data found', but we can ignore this message safely.

We will update our package list for making the new software present for the installation process using the following command:


Install JDK Ubuntu

The installer would see for the Oracle JDK we downloaded in the /var/cache/oracle-jdk11-installer-local directory. We will make this directory and transfer the archive of Oracle JDK there.


Install JDK Ubuntu

Finally, we will install our package using the following command:


Install JDK Ubuntu

First, the installer will ask us for accepting the license agreement of Oracle. We will accept the agreement and the installer would extract the package of Java and install it.

Now, we will see how to choose which Java version we wish to use.

Java Management

We can have more than one installation of Java on a single server. We can configure which release is the default for apply on the terminal with the help of a command, i.e., 'update-alternatives'. The command is mentioned below:


Install JDK Ubuntu

It is what the result will look like if we have installed both releases of Java in this article.

Select the number corresponding to the version of Java to apply it as a default or click on the Enter button for leaving the current settings in position.

We can do it for other commands of Java like the javac compiler. The command is mentioned below:

Many other commands to which this command could be executed include, but aren't restricted to: keytool, jarsigner, and javadoc.

JAVA_HOME Environment Variable Setting

Several programs written in the Java language use the environment variable, i.e., JAVA_HOME for determining the installation location of Java.

First, we need to determine that where the Java version is installed for setting the environment variable.

We will use the following command:


Install JDK Ubuntu

This command will display all installations of Java with the installation path.

In this type of case, the paths of the installation are below:

  1. OpenJDK 11 is positioned at the /usr/lib/jvm/java-11-openjdk-amd64/bin/java directory.
  2. Oracle Java is positioned at the /usr/lib/jvm/java-11-oracle/jre/bin/java directory.

We need to copy the path using our preferred installation. After that, we will open the /etc/environment directory with the help of the nano text editor or any other text editors.

We will add the below line at the completion of this file. Ensure for replacing the highlighted path with our copied path, but don't add the path portion of the bin/.

We will modify this file which will set the path, i.e., JAVA_HOME for every user on our system.

We will save this file and exit from the text editor.

Now, we will reload the file for applying the modifications to our current session:

We will verify that this environment variable has been set with the help of the following command:

We will find the path we just set.

Many other users will require to run the source /etc/environment command or log back and log out to use this setting.


Next Topic#