Java is one of the most popular programming languages and the JVM (Java’s virtual machine) is the run-time environment to run Java applications. These two platforms are required for many popular software that includes Tomcat, Jetty, Cassandra, Glassfish, and Jenkins.
This tutorial will guide you through the installation of Java on Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04, covering both the installation from the Ubuntu repository and the official Oracle package.
Installing Java from the Ubuntu Repository
To begin, search for all available Java versions in the Ubuntu package repository.
apt search openjdk | grep -E 'openjdk-.*-jdk/'
Next, choose the Java version that suits your needs. Here we are installing openjdk-21-jdk (Java Development Kit), which includes the compiler, libraries, and tools needed for developing Java applications.
sudo apt install openjdk-21-jdk
Verify that Java has been installed correctly by checking the version of both the Java runtime and the compiler.
java --version javac --version
If the installation was successful, you should see the version of Java you installed displayed in the terminal.
Installing Java from the Official Oracle Package
Download the Oracle Java package from the official Oracle website or use the following wget command to download directly in the terminal.
wget https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.deb
Once downloaded, use the following dpkg command to install the Oracle Java installer file that includes the necessary binaries and files to run the Java Development Kit on your system.
sudo dpkg -i jdk-22_linux-x64_bin.deb
Verify that Java has been installed correctly by checking the version of both the Java runtime and the compiler.
java --version javac --version
If the installation was successful, you should see the version of Java you installed displayed in the terminal.
Managing Multiple Java Versions in Ubuntu
Having multiple versions of Java installed allows you to test and develop applications using different versions of the Java platform.
You can install multiple versions of Java from the Ubuntu repository and switch between different Java versions using the update-alternatives command as shown.
List all available Java versions installed on your system.
update-alternatives --list java
To switch between different Java versions, use the following command.
sudo update-alternatives --config java
Conclusion
Installing Java on Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04 is a straightforward process that can be accomplished through various methods. This tutorial has covered both the installation from the Ubuntu repository and the official Oracle package.
Additionally, it has discussed how to switch between different Java versions. By following these steps, you can successfully install and manage Java on your Ubuntu system.
Hi everyone,
Ubuntu 24.04 no longer supports running
.jar
files directly. This option is no longer available in the graphical interface, nor is it possible to run Java programs from the command line.Java is installed correctly, as confirmed by running the
javac -version
andjava -version
commands.@Luis,
It sounds like there might be an issue with the Java setup on your system. Even though Java is installed, sometimes Ubuntu requires additional configurations to recognize
.jar
files.Here are a few troubleshooting steps you could try:
1. Run
sudo update-alternatives --config java
to ensure the correct Java version is set as the default.2. Use
java -jar /path/to/yourfile.jar
in the terminal to launch the.jar
file. This should work if Java is properly configured.3. If you haven’t already, try installing OpenJDK (e.g.,
sudo apt install openjdk-17-jdk
) to make sure Ubuntu has all the necessary Java components.Let me know if this helps, or if you’re seeing any specific error messages!”
Hello,
I am having issue. After adding the line JAVA_Home.
bash: /etc/environment: line 2: unexpected EOF while looking for matching `”‘
bash: /etc/environment: line 3: syntax error: unexpected end of file