Java is one of the most well-known programming languages for building different kinds of applications such as Jenkins, Tomcat server, IntelliJ IDEA, and many other system software.
This tutorial will show you the way to install various versions of Java, Java Runtime Environment (JRE) and Java Development Kit (JDK) on Ubuntu 20.04 LTS machine.
Install default java
The simplest way to install Java on your Ubuntu 20.04 is using the default version come along with Ubuntu operating system.
Firstly, let's update the software repository by running the following command:
$ sudo apt update
Next, it's recommended that you should check whether Java is installed or not? Run the command:
$ java -version
The following output indicates that Java has not installed on your Ubuntu 20.04:
Now, you can install the default JRE by executing the following command:
$ sudo apt install default-jre
After the installation has completed, let's verify the Java version:
If you want to compile and run the Java applications, you need a program called 'javac' and it comes with JDK (Java Development Kit). To install default JDK, let's run the following command:
$ sudo apt install default-jdk
You can verify the JDK installation by checking the version of the Java compiler:
$ javac -version
Install OpenJDK 11
At the time of this writing, Java 11 is the latest LTS version. It is the default development and runtime environment of Java on Ubuntu 20.04.
To install OpenJDK 11, run the following command:
$ sudo apt install openjdk-11-jdk
Checking the java version:
$ java -version
Output:
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)
Install OpenJDK 8
In some cases, your applications require the previous version of Java in order to be run. For example, if you want to install Java version 8 on Ubuntu 20.04, run the following command:
$ sudo apt install openjdk-8-jdk
Verifying the java version by typing command 'java -version'.
Output:
openjdk version "1.8.0_252" OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1ubuntu1-b09) OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
Install Oracle Java 11
Unlike the OpenJDK, Oracle JDK is not released under License GPL v2 but Oracle Binary Code License Agreement. By default, the Oracle JDK can not be installed via package managers. You have to download the Oracle JDK 11 installer on Oracle website:
Choose the Linux Compressed Archive, then click Download:
You will be redirected to the login screen of Oracle website. You have to create an account to download Oracle JDK Installer.
After the Java installer has been downloaded (jdk-11.0.8_linux-x64_bin.tar.gz), let's install the 'add-apt-repository' command:
$ sudo apt install software-properties-common
Then, importing the signing key for verifying the software installation on your Ubuntu 20.04:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A
Adding the Java repository to the packages sources list and update:
$ sudo add-apt-repository ppa:linuxuprising/java $ sudo apt update
Creating the local directory for the downloaded Oracle JDK installer:
$ sudo mkdir -p /var/cache/oracle-jdk11-installer-local/ $ sudo cp jdk-11.0.8_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/
Now, you can install the Oracle JDK 11 by running the following command:
$ sudo apt install oracle-java11-installer-local
Let's accept the Oracle Technology Network License Agreement for Oracle Java SE to finish the installation:
Manage multiple Java installations
So far, you've installed several versions of Java on your Ubuntu 20.04. It's necessary to configure the default Java version used by the operating system. You can do it by running the command:
$ sudo update-alternatives --config java
Enter your desired choice then press Enter to choose your preferred Java version.
In order to configure the default Java compiler version (javac), let's run:
$ sudo update-alternatives --config javac
Set Environment Variable
In many cases, when the developers built their Java programs, they have to specify the $JAVA_HOME environment variable.
In order to set the $JAVA_HOME variable, you need to locate the installation directory of Java by using the 'update-alternatives' command.
For example, the Oracle Java is located in '/usr/lib/jvm/java-11-oracle/bin/'.
Add the path to '/etc/environment' file
JAVA_HOME="/usr/lib/jvm/java-11-oracle/bin/"
Reload the environment file for applying the change:
$ source /etc/environment
Verify the $JAVA_HOME environment:
$ echo $JAVA_HOME /usr/lib/jvm/java-11-oracle/bin/
Conclusion
In this tutorial, we learned how to install and configure Java on Ubuntu 20.04. Java is crucial for a number of software applications.
Thanks for reading and please leave your suggestion in the below comment section.
very helpful. when accept the license, I don't how to click "OK" at first, then I found I can press Tab and press Enter.