Java is one of the most popular software and specifications initially created by James Gosling at Sun Microsystems. We can create a cross-platform application in Java for mobile phones or desktop users. Currently, Java is owned by Oracle Corporation. OpenJDK and Oracle Java are the most prominent implementation of Java specifications. Oracle Java comes with restrictive a License and is preferred by enterprise users. OpenJDK is the default on Ubuntu 20.04 LTS and many Linux distros. Let us see how to install Java Runtime Environment (JRE) and Java Development Kit (JDK) on Ubuntu Linux 20.04 LTS.
Tutorial requirements | |
---|---|
Operating system/app | Ubuntu Linux |
Root privileges required | Yes |
Difficulty | Easy (rss) |
Estimated completion time | 5m |
How to install Java on Ubuntu 20.04 LTS
Ubuntu Linux ships with OpenJDK version 8 and 11. Let us find out about versions using the apt-cache command and sort command:
$ apt-cache --names-only search 'openjdk-[0-9]*-(jre|jdk)' | sort -t '-' -k 2 -n
Different versions of JDK and JRE available for installation:
openjdk-8-jdk - OpenJDK Development Kit (JDK) openjdk-8-jdk-headless - OpenJDK Development Kit (JDK) (headless) openjdk-8-jre - OpenJDK Java runtime, using Hotspot JIT openjdk-8-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless) openjdk-8-jre-zero - Alternative JVM for OpenJDK, using Zero/Shark openjdk-11-jdk - OpenJDK Development Kit (JDK) openjdk-11-jdk-headless - OpenJDK Development Kit (JDK) (headless) openjdk-11-jre - OpenJDK Java runtime, using Hotspot JIT openjdk-11-jre-dcevm - Alternative VM for OpenJDK 11 with enhanced class redefinition openjdk-11-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless) openjdk-11-jre-zero - Alternative JVM for OpenJDK, using Zero openjdk-13-jdk - OpenJDK Development Kit (JDK) openjdk-13-jdk-headless - OpenJDK Development Kit (JDK) (headless) openjdk-13-jre - OpenJDK Java runtime, using Hotspot JIT openjdk-13-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless) openjdk-13-jre-zero - Alternative JVM for OpenJDK, using Zero openjdk-14-jdk - OpenJDK Development Kit (JDK) openjdk-14-jdk-headless - OpenJDK Development Kit (JDK) (headless) openjdk-14-jre - OpenJDK Java runtime, using Hotspot JIT openjdk-14-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless) openjdk-14-jre-zero - Alternative JVM for OpenJDK, using Zero
The headless version is for server users where you don’t need desktop GUI components of JAVA.
Step 1 – Installing OpenJDK version 14 on Ubuntu 20.04 LTS
Update the repo and then install the OpenJDK version 14 using the apt command:
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install openjdk-14-jdk
## for servers try headless version ##
$ sudo apt install openjdk-14-jdk-headless
Installing OpenJDK version 14 on Ubuntu server
Step 2 – Verification
Let us print the Java version:
$ java --version
openjdk version "14.0.1" 2020-04-14 OpenJDK Runtime Environment (build 14.0.1+7-Ubuntu-1ubuntu1) OpenJDK 64-Bit Server VM (build 14.0.1+7-Ubuntu-1ubuntu1, mixed mode, sharing)
Step 3 – Install multiple versions of Java/OpenJDK
We can install multiple versions on Ubuntu. It allows us to run different apps as per the Java version. For example, here I am going to install OpenJDK 11:
$ sudo apt install openjdk-11-jdk
Setting up the default Java version
We can use symbolic links to setup the default Java version. For example, change the default Java version, run the update-alternatives command:
$ sudo update-alternatives --config java
Make sure you type the number of the version you want using as the default Java and press the Enter key:
There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-14-openjdk-amd64/bin/java 1411 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode 2 /usr/lib/jvm/java-14-openjdk-amd64/bin/java 1411 manual mode Press to keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/java to provide /usr/bin/java (java) in manual mode
I changed version to JDK 11. Let us print default version:
$ java --version
openjdk 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)
Step 4 – Set up JAVA_HOME environment variable
The syntax is as follows using the export command for bash:
export JAVA_HOME=/path/to/openjdk
# For OpenJDK version 14 #
export JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64
# For OpenJDK version 11 #
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Display the settings using the echo command/printf command:
echo $JAVA_HOME printf "Java HOME PATH : %s\n" $JAVA_HOME
Finally, update the environment variables config file and set JAVA_HOME as follows:
$ sudo vim /etc/environment
## OR ##
$ sudo nano /etc/environment
Append the settings:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Save and close the file.
How to set JAVA_HOME for individual users
The JAVA_HOME set in /etc/environment file is for all Linux users. However, individual users and apps can override JAVA_HOME and PATH settings using the ~/.bash_profile or ~/.bashrc. For instance, Linux user can append PATH settings to end of file as follows:
echo "export JAVA_HOME='/usr/lib/jvm/java-11-openjdk-amd64'" >> ~/.bashrc echo "export PATH='$PATH:/usr/lib/jvm/java-11-openjdk-amd64/bin'" >> ~/.bashrc
Step 5 – Testing your Java version by writing “Hello World” program
Create a new file called HelloWorld.java using a text editor such as vim/nano:
$ vim HelloWorld.java
Append the following code:
// Old good Hello World in Java: // https://docs.oracle.com/javase/tutorial/getStarted/application/index.html import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class HelloWorld { public static void main(final String[] args) { final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("h:mm:ss a 'on' MMMM d, yyyy'.'"); final LocalDateTime now = LocalDateTime.now(); System.out.println("Hello, World! The current time is " + dtf.format(now)); } }
Run it:
java HelloWorld.java
Hello, World! The current time is 8:47:20 AM on September 12, 2020.
Conclusion
This tutorial explained how to install different versions of Java and manage them using the CLI. Once Java is installed, we can write an application. Moreover, your Ubuntu 20.04 LTS is now ready to install other popular opensource apps such as Tomcat.
🐧 6 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Nice tutorial. Can someone explain sort -t '-' -k 2 -n to me?
In other words, I am sorting out apt-cache output using the sort command as per the JDK version so that we can easily read it.
Nice tutorial, but really consider using sdkman for installing Java. The versions are always up to date and installation of both sdkman and the JDK is a breeze.
That is a good suggestion. Thanks!
Why do you trust and recommend sdkman over in built package manager?
Installing is not my problem. Getting javaws to run a .jnlp without throwing an “unsigned application” security error is. I have added exceptions for the url in the java control panel security exception site list; I have commented out disabledAlgorithms lines in java.security. Nothing works, I still get the error trying to launch the .jnlp with javaws.