How to Install Java Development Kit on Ubuntu

admin7 April 2024Last Update :

Understanding the Java Development Kit (JDK)

The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), and other tools needed in Java development. Installing JDK on Ubuntu is a straightforward process that enables developers to create and run Java applications on this popular Linux distribution.

Prerequisites for Installing JDK on Ubuntu

Before proceeding with the installation of the JDK on Ubuntu, there are a few prerequisites that need to be met:

  • A working installation of Ubuntu (preferably the latest version).
  • Access to a user account with sudo privileges.
  • An internet connection to download the JDK package.
  • Basic knowledge of the terminal and command-line operations.

Choosing the JDK Version

There are multiple versions of the JDK available, each suited for different development needs. Oracle JDK and OpenJDK are the two most common distributions. Oracle JDK is the official JDK; however, it is no longer provided for free for commercial use. OpenJDK, on the other hand, is an open-source alternative with a GNU General Public License (GPL). It is important to choose the version that best fits your development requirements.

Oracle JDK vs. OpenJDK

  • Oracle JDK: Known for its performance optimizations and extended support for commercial use (with a paid license).
  • OpenJDK: The community-driven version that is free to use and has a large number of contributors working on its development.

Installing OpenJDK on Ubuntu

OpenJDK is the most widely used JDK version in the development community due to its open-source nature and ease of installation. Here’s how to install it on Ubuntu.

Step 1: Update the Package Index

Before installing any new package, it’s a good practice to update the package index. Open a terminal and execute the following command:

sudo apt update

Step 2: Install OpenJDK

You can install OpenJDK from the Ubuntu repositories. To install OpenJDK 11, which is the current long-term support (LTS) version, use the following command:

sudo apt install openjdk-11-jdk

To install a different version, replace ’11’ with the version number you wish to install.

Step 3: Verify the Installation

After the installation is complete, verify it by checking the version of the Java compiler:

javac -version

The system should display the installed version of the JDK.

Installing Oracle JDK on Ubuntu

For those who prefer Oracle JDK for its performance or need it for specific requirements, here’s how to install it on Ubuntu.

Step 1: Download Oracle JDK

Visit the official Oracle JDK download page and download the appropriate version for your system. You will need to accept the license agreement before the download can begin.

Step 2: Extract and Install Oracle JDK

Once the download is complete, extract the JDK package and move it to a preferred directory, typically /usr/lib/jvm/. Use the following commands to achieve this:

tar -xvf jdk-XX_linux-x64_bin.tar.gz
sudo mv jdk-XX /usr/lib/jvm/jdk-XX

Replace ‘XX’ with the version number of the JDK you downloaded.

Step 3: Update Alternatives

To use the newly installed Oracle JDK as the default, update the alternatives for each tool:

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-XX/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk-XX/bin/javac" 1

Again, replace ‘XX’ with the correct version number.

Step 4: Set Oracle JDK as Default

Set the Oracle JDK as the default using the update-alternatives command:

sudo update-alternatives --config java
sudo update-alternatives --config javac

Select the Oracle JDK from the list provided.

Setting Environment Variables

For Java applications to run correctly, you need to set the JAVA_HOME environment variable. This variable points to the directory where the JDK is installed.

Step 1: Locate the JDK Installation Directory

Find out where Java is installed by using the update-alternatives command:

sudo update-alternatives --config java

Copy the path from the installation you want to use.

Step 2: Set JAVA_HOME Environment Variable

Open the /etc/environment file in a text editor with sudo privileges and add the following line at the end:

JAVA_HOME="/usr/lib/jvm/jdk-XX"

Replace ‘/usr/lib/jvm/jdk-XX’ with the path to your JDK installation.

Step 3: Apply the Changes

To apply the changes, source the environment file or log out and log back in:

source /etc/environment

Verify that JAVA_HOME has been set by echoing its value:

echo $JAVA_HOME

Managing Multiple Java Versions

Developers often need to switch between different Java versions. Ubuntu’s update-alternatives system makes it easy to manage multiple Java installations.

Switching Between Java Versions

To switch between installed Java versions, use the update-alternatives command:

sudo update-alternatives --config java

Choose the version you want to use from the selection menu.

Uninstalling Java

If you need to uninstall Java, you can do so using the apt package manager for OpenJDK or manually for Oracle JDK.

Uninstalling OpenJDK

To uninstall OpenJDK, use the following command:

sudo apt remove openjdk-XX-jdk

Replace ‘XX’ with the version number you want to remove.

Uninstalling Oracle JDK

For Oracle JDK, remove the directory where it was installed:

sudo rm -r /usr/lib/jvm/jdk-XX

Then, remove it from the alternatives:

sudo update-alternatives --remove "java" "/usr/lib/jvm/jdk-XX/bin/java"
sudo update-alternatives --remove "javac" "/usr/lib/jvm/jdk-XX/bin/javac"

Replace ‘XX’ with the correct version number.

Frequently Asked Questions

How do I check if Java is installed on Ubuntu?

You can check if Java is installed by running

java -version

in the terminal. If installed, it will display the version number.

Can I have both Oracle JDK and OpenJDK installed on Ubuntu?

Yes, you can have both installed and switch between them using the update-alternatives command.

Do I need to set JAVA_HOME for Java to work?

While not strictly necessary for Java to work, setting JAVA_HOME is important for some Java-based applications and development tools to function correctly.

Is OpenJDK as good as Oracle JDK?

OpenJDK is the official reference implementation of Java and is fully capable for most development and production environments. Oracle JDK may have some additional performance enhancements and commercial support.

How can I ensure that my JAVA_HOME variable persists across system reboots?

By setting the JAVA_HOME variable in the /etc/environment file, it will persist across reboots.

References

Leave a Comment

Your email address will not be published. Required fields are marked *


Comments Rules :

Breaking News