Explains the steps required to install OpenJDK 13 on Ubuntu 18.04 LTS and getting started with Java development using the hello World example

How To Install OpenJDK 13 On Ubuntu
How To Install OpenJDK 13 On Ubuntu
September 18, 2019

This tutorial provides all the steps required to install OpenJDK 13 on the popular Linux distribution i.e. Ubuntu. It provides all the steps for Ubuntu 18.04 LTS (Bionic Beaver). The steps should be the same for other versions of Ubuntu, and Linux.

You can also follow the steps to install How To Install VSCode For Java On Ubuntu, How To Install IntelliJ IDEA for Java on Ubuntu, and How To Install Eclipse For Java Development On Ubuntu published by Tutorials24x7. You can also follow the How To Install OpenJDK 13 On Windows to install the latest OpenJDK on Windows and How To Install Java 13 On Windows to install Oracle JDK.

Notes: You may follow What's New In Java 13 to know about the new features introduced in JDK 13.

Download JDK

Open the JDK 13 GA Release site and download the distribution for Linux systems as shown in Fig 1.

OpenJDK 13 Download

Fig 1

Install JDK

Open the terminal and make the directory /usr/java to keep all the java installations at the same place. We can install multiple versions of Java in this directory. Now copy the downloaded file to this location and extract it as shown below:

>sudo mkdir /usr/java
>cd /usr/java
>sudo cp /m1data/setups/openjdk-13_linux-x64_bin.tar.gz openjdk-13_linux-x64_bin.tar.gz
>sudo tar -xzvf openjdk-13_linux-x64_bin.tar.gz

The above steps will install JDK to the path /usr/java/jdk-13.

Set Environment Variables

In this step, we will configure the environment variable to use the JDK installed by us.

>sudo nano /etc/profile

Scroll down by pressing Page Down button and add at the end of this file:

JAVA_HOME=/usr/java/jdk-13
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

Now press Ctrl + O and press Enter to write our change. Press Ctrl + X to exit the nano editor. The nano editor should look like the one shown in Fig. 2.

OpenJDK 13 System Path

Fig 2

Configure Java Commands

We can configure the Java commands to use the newly installed JDK by default. We can check the installed Java before and after executing these commands as shown below:

# Check version
java -version
# Configure Java Alternatives
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/java/jdk-13/bin/java" 1
# Configure Javac Alternatives
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/jdk-13/bin/javac" 1
# Check version
java -version

# Use only in case of multiple JDKs installed

# Configure Java
sudo update-alternatives --config java

# Configure Java Compiler
sudo update-alternatives --config javac

The output of these commands is shown in Fig. 3. We might need to configure active Java if it is previously installed on the system.

OpenJDK 13 Configure Commands

Fig 3

These are the basic steps required to install the most recent version of Java on Ubuntu.

Hello Java

In this section, we will write our first program in Java using the nano editor.us.

>sudo mkdir /m1data/programs
>cd /m1data/programs
>sudo nano HelloJava.java

Now write the first Java program as shown in Fig 4, save the program and exit the editor.

Hello Java

Fig 4

Use the javac and java to compile and execute the program as shown below.

// Compile the program
sudo javac HelloWorld.java

// Execute the program
sudo java HelloWorld

// Program output
Hello Java !!

These are the basic steps to write, compile, and execute Java programs.

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS