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: Now, choose what Java version you want to install and execute one of the following commands to install that specific version.
Install OpenJDK 11sudo apt install openjdk-11-jdk Install OpenJDK 8sudo apt install openjdk-8-jdk Install OpenJDK 17sudo 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.
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: 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. 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: 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: 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.
Conclusion
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.