Java and the JVM (Java's virtual machine) are required for many kinds of software, including Tomcat, Jetty, Glassfish, Cassandra and Jenkins.
Basically, 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.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Java queries on any Linux distribution.
In this context, we shall look into how to install Java Run-time Environment (JRE) and the Java Developer Kit (JDK) on Ubuntu 20.04 needed by many Java-based applications.
There are four Java platforms available:
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:
-bash: java: command not found
How to Install 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
How to Install 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.
How to Configure the JAVA_HOME Environment Variable ?
The JAVA_HOME environment variable is used to discover the Java installation directory by Java-based applications.
i. 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
ii. Now open /etc/environment file using your favorite text editor:
$ sudo nano /etc/environment
iii. 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"
iv. Save the file, then reload the configuration to your current session:
$ source /etc/environment
v. 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.
This article covers how to install Java Run-time Environment (JRE) and the Java Developer Kit (JDK) on Ubuntu 20.04. Java is one of the most popular programming languages. It is used for developing anything from lightweight mobile to desktop applications.
Oracle's licensing agreement for Java doesn’t allow automatic installation through package managers. To install the Oracle JDK, which is the official version distributed by Oracle, you must create an Oracle account and manually download the JDK to add a new package repository for the version you'd like to use.
To install the Default JRE/JDK
1. To install this version, first update the package index:
$ sudo apt update
2. Next, check if Java is already installed:
$ java -version
If Java is not currently installed, you’ll see the following output:
Output
Command 'java' not found, but can be installed with:
apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headless
3. Execute the following command to install the default Java Runtime Environment (JRE), which will install the JRE from OpenJDK 11:
$ sudo apt install default-jre
The JRE will allow you to run almost all Java software.
4. Verify the installation with:
java -version