Java is a widely used programming language and runtime environment that is essential for many software applications. OpenJDK (Open Java Development Kit) is an open-source implementation of the Java Platform, Standard Edition. If you’re running Ubuntu 22.04 and need Java for development or application deployment, this guide will walk you through the installation process.
Before installing any new software, it is always recommended to update the package index to ensure you get the latest versions. Open a terminal and run the following command:
sudo apt update && sudo apt upgrade -y
This will update the package lists and upgrade installed packages to their latest versions.
Ubuntu 22.04 provides multiple versions of OpenJDK. You can choose to install OpenJDK 11 or OpenJDK 17, which are commonly used versions.
To install OpenJDK 11, run:
sudo apt install openjdk-11-jdk -y
To install OpenJDK 17, run:
sudo apt install openjdk-17-jdk -y
You can install multiple versions if needed, but only one can be set as the default.
After installation, verify that Java has been installed correctly by checking the version:
java -version
This command should output something like:
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment (build 17.0.8+7-Ubuntu-222.04)
OpenJDK 64-Bit Server VM (build 17.0.8+7-Ubuntu-222.04, mixed mode, sharing)
If you have multiple Java versions installed, you can set the default version using the following command:
sudo update-alternatives --config java
This will display a list of installed Java versions. Select the number corresponding to the version you want to set as the default.
To check the current default Java version, use:
java -version
Many applications require the JAVA_HOME
environment variable to be set. To find out the Java installation path, run:
readlink -f $(which java)
Once you have the path, edit the environment file:
sudo nano /etc/environment
Add the following line at the end:
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
Replace the path with the actual installation path of your Java version. Save the file and reload it:
source /etc/environment
To verify, run:
echo $JAVA_HOME
If you need to remove OpenJDK for any reason, use the following command:
sudo apt remove --purge openjdk-17-jdk -y
Replace openjdk-17-jdk
with the installed version you want to remove.
Installing OpenJDK on Ubuntu 22.04 is a straightforward process. Whether you need Java for software development, running applications, or server-side processes, following the steps above will ensure you have a properly configured Java environment.