How To

Install Java on Ubuntu 18.04: OpenJDK 11 and OpenJDK 8

Many modern programs require Java to run. From development tools like Eclipse to search systems like Elasticsearch, Java is a core package for web hosts.

There are different versions of Java available. You can use OpenJDK, which is the open source version, or the official Oracle Java. For almost all tasks, OpenJDK is the best choice because it is free to use and easy to install.

This tutorial will show you how to install Java on Ubuntu 18.04, choose a default version, and set up your system environment.

Install the Default Java Version: OpenJDK 11

The default Java version in Ubuntu 18.04 is OpenJDK 11. This version is a long-term support release and is the standard for most modern systems.

Open your terminal and update the package list:

bashsudo apt update

Install the full development kit by running:

bashsudo apt install default-jdk

Verify that the installation was successful:

bashjava -version

The output will display the installed Java version. If you only need to run Java apps and do not need to build them, you can install the runtime environment instead by running sudo apt install default-jre.

Install OpenJDK 8

Some older programs do not work with newer Java versions. If you run software that requires Java 8, you can install OpenJDK 8 on the same machine.

Update your package index and run this install command:

bashsudo apt updatesudo apt install openjdk-8-jdk

Confirm the installation by checking the version again:

bashjava -version

The output will show version 1.8.0 to confirm that the package was installed correctly.

Choose the Default Java Version

If you have both Java 11 and Java 8 on your server, you can choose which one the system uses by default.

Run the configuration tool:

bashsudo update-alternatives --config java

The terminal will print a list of all installed Java versions. Each version has a number next to it. Type the selection number for the version you want to use and press Enter.

Set the JAVA_HOME Variable

Many Java programs look for the JAVA_HOME variable to find where Java is installed.

First, locate the path of your default Java version using the configuration command from the previous section. Copy the path to the directory, leaving out the /bin/java part.

Open the system environment file:

bashsudo nano /etc/environment

Add the following line at the end of the file:

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

Save your changes. Reload the file to apply the environment changes to your current session:

bashsource /etc/environment

Confirm the path by printing the variable:

bashecho $JAVA_HOME

You have installed Java and set up your system path. Now you can run Java apps or start building your own. If you have any questions about this guide, leave a comment below.

Cyber Defence

Recent Posts

Install Mono on Ubuntu 18.04: C# Compiler and Runtime Guide

Running programs built for Microsoft's framework on a Linux system is easier than you think. Mono is…

41 minutes ago

Install OpenCV on Ubuntu 18.04: Step-by-Step Setup Guide

Computer vision technology powers many modern applications, from image editors to facial scanners. OpenCV (Open Source Computer…

52 minutes ago

Install VNC on Ubuntu 18.04: Step-by-Step TigerVNC Setup

A remote desktop interface makes it easy to manage a remote computer. VNC (Virtual Network Computing) is…

1 hour ago

Install Gitea on Ubuntu 18.04: Self-Hosted Git Service Guide

Hosting your own code repositories is a great way to keep your projects private. Gitea is a…

1 hour ago

Configure a Static IP Address on Ubuntu 18.04: Netplan Guide

Setting a static IP address on your server is a smart move. It ensures your…

1 day ago

Install Xrdp on Ubuntu 18.04: Remote Desktop Setup Guide

Xrdp is an open-source implementation of the Microsoft Remote Desktop Protocol (RDP). It lets you access…

1 day ago