How to Install Java 8 on Ubuntu 20.04

install-java-8-ubuntu-20.04

Java is Object Oriented Programming language as well as the platform developed by James Gosling at Sun Microsystems. It is a first programming language which provides the concept of writing programs that can be executed using the web. There are many devices where java is currently used including, Desktop Applications, Web Applications, Enterprise Applications, Mobile Applications, Embedded System, Games, Robotics and many more.

Following are some of the notable feature of Java:

  • Simple, Easy to Use and Easy to learned.
  • Object-oriented and Platform independent.
  • Secure, Portable and Robust.
  • Multithreaded and Distributed.

There are two major implementations of Java, OpenJDK and Oracle Java. The OpenJDK is a free version of Java while the Oracle Java has a few additional commercial features. You can use Oracle Java for non-commercial use like personal use and development use.

At the time of writing this tutorial, the latest available version of Java is Java SE 15. Some applications are based on the specific Java version. In that case, you may need to install the specific version of Java.

In this tutorial, we will learn the following:

  1. How to Install Java 11 on Ubuntu 20.04.
  2. How to Install Java 8 on Ubuntu 20.04.
  3. How to Install Oracle Java 11 on Ubuntu 20.04.
Also Read

Top 10 Dedicated Server Hosting

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is set up in your server.

Install Java 11 on Ubuntu 20.04

As of now, the OpenJDK 11 is the long term support (LTS) version of Java. By default, it is available in the Ubuntu 20.04 standard repository. You can install it easily by running the following command:

1
2
apt-get update -y
apt-get install openjdk-11-jdk -y

After the installation, verify the installed version of Java by running the following command:

1
java -version

You should see the installed Java version in the following output:

1
2
3
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

If you want to install only Java Runtime Environment (JRE) or minimal Java runtime, run the following command:

1
apt-get install openjdk-11-jre openjdk-11-jdk-headless -y

Install Java 8 on Ubuntu 20.04

You may also need to install the OpenJDK 8 in your system if your application is based on the java version 8. You can simply run the following command to install Java 8 in your system.

1
2
apt-get update -y
apt-get install openjdk-8-jdk -y

After the installation, verify the installed version of Java by running the following command:

1
java -version

You should see the installed Java version in the following output:

1
2
3
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0ubuntu2~20.04-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)

Install Oracle Java on Ubuntu 20.04

Oracle Java is a commercial version Java with some additional features. You can use it only for personal or development use. By default, the Oracle Java is not included in the Ubuntu 20.04 repository. So you will need to create an Oracle account and download your desired version of Java from the Oracle website.

First, Log in and visit the Oracle Downloads page and locate your desired version of Java. In this case, we will install Java SE 11 LTS as shown below:

download-java

Next, click on the JDK Download button. You should see the page that shows the available versions:

jdk-version-11

Select the jdk-11.0.8_linux-x64_bin.tar.gz package for Linux and download it on your system.

Next, create a directory for Java installation:

1
mkdir -p /var/cache/oracle-jdk11-installer-local

Next, copy the downloaded file to that directory:

1
cp jdk-11.0.8_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/

Next, install the required dependencies in your system with the following command:

1
apt-get install software-properties-common gnupg2 -y

Next, import and sign the key with the following command:

1
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A

You should get the following output:

1
2
3
4
Executing: /tmp/apt-key-gpghome.xtQ8FoH3wx/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A
gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" imported
gpg: Total number processed: 1
gpg:               imported: 1

Next, add the Java installer repository with the following command:

1
add-apt-repository ppa:linuxuprising/java

Next, update the repository and install the Oracle Java 11 SE with the following command:

1
2
apt-get update -y
apt-get install oracle-java11-installer-local -y

After the installation, verify the installed version of Java by running the following command:

1
java -version

You should see the installed Java version in the following output:

1
2
3
java version "11.0.8" 2020-07-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)

Set or Change the Java Default Version

If you are using multiple version of Java in your system. You can easily set or change the default Java version as per your development needs.

To change the default Java version, run the following command:

1
update-alternatives --config java

You should see that there are three Java versions installed in your system and active version is Oracle Java 11.

01
02
03
04
05
06
07
08
09
10
There are 3 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-11-oracle/bin/java             1091      manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
 
Press  to keep the current choice[*], or type selection number: 1

Type the number of the installed version that you want to set as a default version and hit Enter.

You will also need to do this for other Java commands including javac as shown below:

1
update-alternatives --config javac

Set JAVA_HOME Environment Variable

Some applications use the JAVA_HOME environment variable to determine the Java installation location. In this case, you will need to set the JAVA_HOME environment variable for your default Java version.

First, you will need to locate the path the Java installation. You can find it using the following command:

1
update-alternatives --config java

You should see the following output:

1
2
3
4
5
6
7
8
There are 3 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-11-oracle/bin/java             1091      manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

As you can see, the path of the all Java version are as follows:

  1. OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/.
  2. Oracle Java is located at /usr/lib/jvm/java-11-oracle/.
  3. OpenJDK 8 is located at /usr/lib/jvm/java-8-openjdk-amd64/.

Next, you will need to copy the path of your desired Java version and add it to the /etc/environment file:

1
nano /etc/environment

Add the following line for Oracle Java 11 version:

1
JAVA_HOME="/usr/lib/jvm/java-11-oracle/"

Save and close the file then activate the environment variable using the following command:

1
source /etc/environment

Next, you can verify the Environment variable which you have set using the following command:

1
echo $JAVA_HOME

You should see the following output:

1
/usr/lib/jvm/java-11-oracle/

Create Your First Java Program

In this section, we will show you how to compile and run your first Java program.

First, create a new file named JavaApp.java with the following command:

mkdir project
cd project
nano JavaApp.java

Add the following code:

class HelloWorld {
 public static void main(String args[]){
     System.out.println("Hello World");
 }
}

Save and close the file then compile the code using the following command:

javac JavaApp.java

Once the program has been compiled, you can see that a file named HelloWorld.class has been created.

ls -l

Output:

total 8
-rw-r--r-- 1 root root 422 Dec  6 07:02 HelloWorld.class
-rw-r--r-- 1 root root 106 Dec  6 07:02 JavaApp.java

Now, run the command java followed by the class name as shown below:

java HelloWorld

You should see the following output:

Hello World
You should also read the following article.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva