Java is the most popular object-oriented, robust, platform-independent programming language. A large number of applications developed on Java required a system has Java installed.
OpenJDK is an open-source implementation of Java available freely for production use.
This guide will help you to install Java (OpenJDK 17, 11, and 8) LTS releases. Also, help you to install Oracle Java 17 LTS) on your Ubuntu system. You will also find the instruction’s to switch between multiple installed Java versions.
Installing Java on Ubuntu
Java 17 is the latest LTS release available for installation. JDK packages provide the full Java development libraries, helpful for the development systems. To run a Java application, you just needed a Java runtime environment (JRE).
1. Install Java (OpenJDK) from Default Repositories
Default Ubuntu 22.04 repositories contain the OpenJDK 18, 17, 11, and 8 packages. By default, the apt package manager will install Java 11 but you can also install a specific Java version.
First of all, update the Apt cache:
sudo apt update
Now, choose what Java version you want to install and execute one of the following commands to install that specific version.
- Install OpenJDK 11
sudo apt install openjdk-11-jdk
- Install OpenJDK 8
sudo apt install openjdk-8-jdk
- Install OpenJDK 17
sudo apt install openjdk-17-jdk
You can also install OpenJDK 18 (openjdk-18-jdk) package, but that is a non-LTS release.
Once the installation is complete, you can verify it by checking the Java version with the below command.
java -versionopenjdk version "11.0.15" 2022-04-19 OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1) OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)
2. Install Oracle Java 17 on Ubuntu
As of today, Oracle Java 18 and Java 17 are available to download and install without getting a commercial license. Java 18 is a non-LTS version and Java 17 is an LTS version. So we prefer to install Java 17 on Ubuntu systems.
You can visit official download page to download Debian file on your system. Also, you can use the below command to download the Debian file using the command line.
If the above command does not work or you found a file not found error, please go to the official download page. Check for the current version and download it to your system.
Once you download the Debian file, install it on your system using command below:
sudo apt install ./jdk-17_linux-x64_bin.deb
The above command installs Oracle Java 17 on your system. All the files will be placed under
/usr/lib/jvm directory. Now set an alternative reference to newly installed Java on your system.
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 100
Oracle Java 17 has been installed on your Ubuntu system. If you have installed multiple Java versions on a single system, go to the next step to change the default Java version with update-alternatives CLI on a Ubuntu system.
Change Default Java Version on Ubuntu
Most Unix/Linux-based systems allow the installation of multiple Java versions on one system. If you also have multiple Java versions installed on your system. You can change to the default java version as per your requirements.
The update-alternatives provides you option to maintain symbolic links for the default commands. To change default Java version run command on terminal:
update-alternatives --config java
This will show the list of all java binaries installed on your system. Enter a number to select the default Java version and press enter. See the below screenshot:
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-8-openjdk-amd64/jre/bin/java 1081 manual mode 3 /usr/lib/jvm/jdk-17/bin/java 100 manual mode Press
to keep the current choice[*], or type selection number: 3
The above command will change the default Java version on your system by changing the link references to the java binary. Now, again run command java -version to view the default Java version.
java -versionjava version "126.96.36.199" 2022-04-22 LTS Java(TM) SE Runtime Environment (build 188.8.131.52+2-LTS-6) Java HotSpot(TM) 64-Bit Server VM (build 184.108.40.206+2-LTS-6, mixed mode, sharing)
In this tutorial, you have learned about the installation of multiple Java on the Ubuntu 22.04 systems. Also found a solution to change the default Java version via the command line.