Installing Java Development Kit (JDK) 11 on Ubuntu

Installing Java Development Kit (JDK) 11 on Ubuntu

Java is a widely-used, object-oriented programming language, and the Java Development Kit (JDK) provides the tools necessary to develop and run Java applications. This tutorial details how to install JDK 11 on an Ubuntu system.

Prerequisites

  • An Ubuntu system (tested on versions 18.04, 20.04, and 22.04).
  • A user account with sudo privileges.
  • An active internet connection.

Installation Methods

There are several methods for installing JDK 11 on Ubuntu. Here, we’ll cover the most common approaches: using apt and utilizing sdkman.

1. Using apt (The Package Manager)

The simplest and most common method involves using Ubuntu’s package manager, apt.

  • Update Package Lists: First, update the package lists to ensure you have the latest information about available packages.

    sudo apt update
    
  • Install the Default JDK: Install the default JDK using the following command:

    sudo apt install default-jdk
    

    This command typically installs the latest available JDK, which, at the time of writing, includes JDK 11.

  • Alternatively, Install OpenJDK 11 Directly: To specifically install OpenJDK 11, use:

    sudo apt install openjdk-11-jdk
    
  • Using a PPA (Personal Package Archive): For the most up-to-date OpenJDK 11 packages, consider using a PPA:

    sudo add-apt-repository ppa:openjdk-r/ppa
    sudo apt update
    sudo apt install openjdk-11-jdk
    

2. Using sdkman (Software Development Kit Manager)

sdkman is a useful tool for managing multiple JDKs and other software development kits. It allows you to easily switch between different versions and installations.

  • Install sdkman:

    curl -s "https://get.sdkman.io" | bash
    source "$HOME/.sdkman/bin/sdkman-init.sh"
    
  • List Available Java Versions:

    sdk list java
    
  • Install Java 11: Choose a Java distribution (e.g., Zulu, Liberica, etc.) and install it using the version number. For example:

    sdk install java 11.0.3-zulu #Replace with desired distribution and version
    

    Or simply:

    sdk install java
    

    which will install the latest stable version.

Verifying the Installation

After installation, verify that Java is installed correctly by checking its version.

java -version

This command should output the installed Java version, including "11". For example:

openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3, mixed mode, sharing)

Switching Between Java Versions (If Multiple are Installed)

If you have multiple Java versions installed, you might need to switch between them. The method for doing this depends on how you installed Java.

  • Using update-alternatives: If you installed Java using apt, you can use the update-alternatives command to switch between versions. First, list the available Java installations:

    update-java-alternatives --list
    

    Then, configure the desired Java version:

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

    Follow the on-screen prompts to select the desired Java installation.

  • Using sdkman: If you installed Java using sdkman, simply use the following command to switch to a different version:

    sdk use java <version>
    

    Replace <version> with the desired Java version number.

Setting JAVA_HOME (Optional)

Some applications require the JAVA_HOME environment variable to be set. To set it:

  1. Open your ~/.bashrc or ~/.profile file.

  2. Add the following line, replacing /usr/lib/jvm/java-11-openjdk-amd64 with the actual path to your Java installation:

    export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
    export PATH=$JAVA_HOME/bin:$PATH
    
  3. Save the file and source it to apply the changes:

    source ~/.bashrc
    # or
    source ~/.profile
    

Leave a Reply

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