How to Install Java on Ubuntu

Install Java on Ubuntu

Java is a widely-used programming language that powers a vast array of applications, from mobile apps and games to enterprise software and big data solutions. If you’re a developer working on Ubuntu, having Java installed on your system is essential for building and running Java-based programs. In this comprehensive guide, we’ll walk you through the step-by-step process of installing Java on Ubuntu, covering different versions and installation methods.

Whether you‘re a seasoned developer or just starting your coding journey, this article will provide you with the knowledge and tools you need to get Java up and running on your Ubuntu machine. We’ll cover everything from checking if Java is already installed to setting up the Java environment variables, ensuring you have a solid foundation for your Java development projects.

Understanding Java Versions

Before we dive into the installation process, it’s important to understand the different Java versions available and their respective use cases. Java comes in two main flavors: Java Runtime Environment (JRE) and Java Development Kit (JDK).

Java Runtime Environment (JRE) is a software package that provides the necessary components to run Java applications. It includes the Java Virtual Machine (JVM), which is responsible for executing Java bytecode, as well as the core Java libraries and other supporting files.

Java Development Kit (JDK), on the other hand, is a more comprehensive package that includes the JRE, as well as additional tools and utilities for developing Java applications. The JDK includes the Java compiler, debugger, and other development tools that are essential for writing, compiling, and debugging Java code.

For most development purposes, you’ll want to install the JDK, as it provides the complete set of tools required for building Java applications. However, if you only need to run pre-compiled Java programs, the JRE may be sufficient.

Checking for Existing Java Installation

Before proceeding with the installation, it’s a good idea to check if Java is already installed on your Ubuntu system. Open a terminal window and enter the following command:

java -version

If Java is installed, you’ll see output similar to this:

openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu222.04, mixed mode, sharing)

This output shows that OpenJDK 11 is installed on the system. If Java is not installed, you’ll receive a message stating that the java command was not found.

Installing the Default JRE/JDK

If Java is not installed on your Ubuntu system, you can easily install the default version of the Java Runtime Environment (JRE) or the Java Development Kit (JDK) using the Ubuntu package manager, apt.

Installing the Default JRE

To install the default JRE package, open a terminal window and run the following command:

sudo apt update
sudo apt install default-jre

The sudo apt update command updates the package index, ensuring you have the latest package information. The sudo apt install default-jre command installs the default JRE package on your system.

Installing the Default JDK

If you need the full Java Development Kit, you can install it with the following command:

sudo apt install default-jdk

This command will install the default JDK package, which includes the JRE and additional development tools.

After the installation is complete, you can verify that Java is installed by running the java -version command again. You should see output similar to the example shown earlier, indicating the installed Java version.

Installing a Specific Java Version

While the default Java packages provided by Ubuntu are suitable for many use cases, you may need to install a specific Java version for compatibility with certain applications or development environments. In this section, we’ll cover how to install a specific Java version on Ubuntu.

Adding a Third-Party Repository

To install a specific Java version, you’ll need to add a third-party repository to your Ubuntu system. The process varies depending on the Java version you want to install, but we’ll use OpenJDK 11 as an example.

First, update the package index:

sudo apt update

Next, add the OpenJDK 11 repository to your system’s sources list:

sudo apt install software-properties-common
sudo add-apt-repository ppa:openjdk-r/ppa

The software-properties-common package provides an easy way to manage software repositories, while the add-apt-repository command adds the OpenJDK 11 repository to your system.

After adding the repository, update the package index again:

sudo apt update

Installing the Desired Java Package

With the repository added, you can now install the desired Java package. For OpenJDK 11, run the following command:

sudo apt install openjdk-11-jre

This command installs the OpenJDK 11 Java Runtime Environment (JRE). If you need the full Java Development Kit (JDK), replace jre with jdk:

sudo apt install openjdk-11-jdk

After the installation is complete, you can verify the installed Java version by running java -version in the terminal.

Installing Oracle Java

While OpenJDK is a popular and widely-used open-source implementation of the Java platform, some applications or development environments may require the proprietary Oracle Java version. In this section, we’ll cover how to install Oracle Java on Ubuntu.

Downloading the Oracle JDK

First, visit the Oracle Java Downloads page and download the appropriate Java Development Kit (JDK) package for your system. For this example, we’ll use the Oracle JDK 11 package for Linux x64. Once the download is complete, navigate to the directory where the downloaded file is located using the terminal.

Installing the Oracle JDK Package

To install the Oracle JDK package, you’ll need to use the dpkg command. Assuming the downloaded file is named jdk-11.0.2_linux-x64_bin.deb, run the following command:

sudo dpkg -i jdk-11.0.2_linux-x64_bin.deb

This command installs the Oracle JDK package on your system.

After the installation is complete, update the package index:

sudo apt update

If you encounter any dependency issues during the installation process, you can try to fix them by running the following command:

sudo apt --fix-broken install

Setting the Default Java Version

If you have multiple Java versions installed on your Ubuntu system, you may need to set the default Java version to ensure that the correct version is used when running Java applications or development tools.

To set the default Java version, run the following command:

sudo update-alternatives --config java

This command will display a list of installed Java versions, along with their respective selection numbers. Enter the selection number corresponding to the Java version you want to set as the default, and press Enter.

After setting the default Java version, you can verify the change by running java -version in the terminal.

Setting the JAVA_HOME Environment Variable

Many Java applications and development tools rely on the JAVA_HOME environment variable to locate the Java installation directory. Setting this variable can help ensure that your Java applications and tools run smoothly.

Finding the Java Installation Path

First, you’ll need to find the installation path for your Java version. You can do this by running the following command:

update-java-alternatives -l

This command will list all installed Java versions and their respective installation paths. Look for the path corresponding to the Java version you want to use, and make a note of it.

Editing the Environment File

Next, open the /etc/environment file using a text editor with root privileges. For example, you can use the nano editor:

sudo nano /etc/environment

In the environment file, add the following line, replacing /path/to/java with the actual installation path you noted earlier:

JAVA_HOME="/path/to/java"

Save the changes and exit the text editor.

Reloading the Environment File

After modifying the environment file, you’ll need to reload it for the changes to take effect. You can do this by running the following command:

source /etc/environment

Verifying the JAVA_HOME Setting

To verify that the JAVA_HOME environment variable is set correctly, run the following command:

echo $JAVA_HOME

This command should output the Java installation path you specified in the /etc/environment file.

Testing the Java Installation

After completing the installation and configuration steps, it’s a good idea to test your Java installation to ensure everything is working correctly.

Compiling and Running a Simple Java Program

Create a new file called HelloWorld.java with the following content:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Save the file and navigate to the directory containing the HelloWorld.java file in the terminal.

To compile the Java program, run the following command:

javac HelloWorld.java

If the compilation is successful, you should see a new file called HelloWorld.class in the same directory.

To run the compiled Java program, use the following command:

java HelloWorld

Troubleshooting Common Issues

While the installation process is generally straightforward, you may encounter some common issues. Here are a few troubleshooting tips:

Java Command Not Found

If you receive an error message stating that the java command was not found, it’s likely that Java is not installed or the installation was unsuccessful. Double-check your installation steps and ensure that the Java binaries are included in your system’s PATH environment variable.

Version Conflicts

If you have multiple Java versions installed on your system, you may encounter version conflicts when running certain applications or development tools. In this case, you can try setting the default Java version or specifying the desired Java version explicitly when running the application or tool.

Dependency Issues

During the installation process, you may encounter dependency issues, particularly when installing Oracle Java or specific Java versions from third-party repositories. If this happens, try running the sudo apt --fix-broken install command to resolve any missing dependencies.

Environment Variable Issues

If you’re experiencing issues with Java applications or development tools not recognizing the correct Java installation, double-check your JAVA_HOME environment variable setting. Ensure that the variable is set correctly and that you’ve reloaded the environment file after making changes.

Conclusion

Installing Java on Ubuntu is a straightforward process, but it’s essential to follow the correct steps and understand the different Java versions and installation methods. By following the instructions in this guide, you should now have Java installed and configured on your Ubuntu system, ready for your development projects.

Remember to set the default Java version and configure the JAVA_HOME environment variable for a seamless development experience. Additionally, testing your Java installation by compiling and running a simple program can help ensure that everything is working correctly.

Whether you’re a seasoned Java developer or just starting your coding journey, having a solid understanding of Java installation and configuration on Ubuntu will serve you well. With this knowledge, you can focus on building amazing Java applications and exploring the vast possibilities of this powerful programming language.

Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts