There are a lot of Java applications that you may come across and use in your daily work. That’s why you need JVM (Java’s virtual machine) as the run-time environment to run Java applications.
This tutorial will show you how to install Java Run-time Environment (JRE) and the Java Developer Kit (JDK) on Ubuntu 20.04 needed by many Java-based applications.
Installing the Default JRE in Ubuntu
We will install OpenJDK 11, which is an open-source alternative to the JRE and JDK.
First, we update the system with:
$ sudo apt update && sudo apt upgrade
Now we will check if the Java Run-time Environment is installed on the system:
$ java -version
If Java is not installed on your system you will get the command not found error:
Installing Java Runtime Environment (JRE)
Now we will install OpenJDK 11, which will provide Java Runtime Environment (JRE). Type the following command to install the packages:
$ sudo apt install default-jre
Type ‘Y’ to continue.
After the packages are installed, type the java -version command again to verify the operation was successful:
$ java -version
Installing JDK (Java Development Kit)
After we have successfully installed Java Run-time Environment, we can now install Java Development Kit (JDK).
Type the following command to install it:
$ sudo apt install default-jdk
We can now verify the Java Development Kit installation by typing the following:
$ javac -version
If everything went ok, we should get the installed JDK version:
Next, we need to set up the JAVA_HOME environment variable.
Setting the JAVA_HOME Environment Variable
The JAVA_HOME environment variable is used to discover the Java installation directory by Java-based applications.
First, we must check in which directory the Java is installed:
$ readlink -f /usr/bin/java
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
Now open /etc/environment file using your favorite text editor:
$ sudo nano /etc/environment
Now add the following line to the end of the file, after replacing the path with the installation directory path on your system that you’ve got by running the previous command readlink, but omit /bin/java at the end:
JAVA_HOME=”/usr/lib/jvm/java-11-openjdk-amd64”
Save the file, then reload the configuration to your current session:
$ source /etc/environment
Now verify the environment path is updated by typing the following command:
$ echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-amd64
You should see the java path in the terminal output like in the example above.
Conclusion
In this tutorial, we have learned how to install Java Run-time Environment (JRE) and the Java Developer Kit (JDK) on Ubuntu 20.04 and the same steps are applicable on other Ubuntu versions as well.