Install OpenJDK 17 on Ubuntu 22.04: Free JDK – OrcaCore

Posted on

Install OpenJDK 17 on Ubuntu 22.04: Free JDK – OrcaCore

This tutorial will guide you through the process of how to Install OpenJDK 17 on Ubuntu 22.04. OpenJDK is a free, open-source version of the Java Development Kit for the Java Platform, Standard Edition (Java SE). Originating from an effort initiated by Sun Microsystems in 2006, OpenJDK, which stands for Open Java Development Kit, is now sponsored and led by Oracle. The project operates under the GNU General Public License (GNU GPL) version 2 with a linking exception. This exception is crucial; without it, components linking to the Java class library would be bound by the terms of the GPL license.

You can now follow the steps below on the Orcacore website to start your OpenJDK 17 installation on Ubuntu 22.04.

To successfully complete this guide, you’ll need to log in to your server as a non-root user with sudo privileges. If you haven’t already done so, you can refer to our guide on Initial Server Setup with Ubuntu 22.04.

Method 1. Download OpenJDK 17 in the Latest Archive

First, update your local package index using the following command:

sudo apt update

Now, visit the JDK Downloads page to download the latest archive. Utilize the wget command:

sudo wget https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz

Next, extract the downloaded file with this command:

sudo tar xvf openjdk-17.0.2_linux-x64_bin.tar.gz

Move the extracted file to the /opt directory:

sudo mv jdk-17.0.2 /opt/

Configure Java Environment Path on Ubuntu 22.04

Configure the Java home path using the following command:

sudo tee /etc/profile.d/jdk17.sh <<EOF
export JAVA_HOME=/opt/jdk-17.0.2
export PATH=$PATH:$JAVA_HOME/bin
EOF

Source your profile file:

source /etc/profile.d/jdk17.sh

Verify your Java Home path:

echo $JAVA_HOME
**Output**
/opt/jdk-17.0.2

Verify your Java installation by checking its version:

java -version
1. OpenJDK 17 on Ubuntu 22.04

Method 2. Install OpenJDK 17 From Ubuntu Apt Repository

Another method to install Java 17 is using the Apt repository on Ubuntu 22.04.

First, update your local package index:

sudo apt update

Then, install Java 17:

sudo apt install openjdk-17-jre-headless -y

Verify your Java installation by checking its version:

java -version

Test OpenJDK 17 Installation

To verify that your Java installation is working correctly, create a sample project.

Create and open a "hello world" file with your preferred text editor (here, the vi editor is used):

vi HelloWorld.java

Add the following content to the file:

public class HelloWorld {

    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");
    }

}

Save and close the file.

Compile and run your Java code:

java HelloWorld.java
**Output**
Hello, World

That’s it, you are done. You have successfully installed Install OpenJDK 17 on Ubuntu 22.04.

Conclusion

You have now learned how to Install OpenJDK 17 on Ubuntu 22.04. This process involves updating the system, using the APT package manager, or downloading OpenJDK 17 in the latest archive, and verifying the installation. Installing OpenJDK 17 on Ubuntu 22.04 opens up the door to developing and running Java-based applications.

Hope you enjoy it. You may also be interested in these articles:

  • How To Install OTRS on Ubuntu 22.04
  • Set up PowerDNS on Ubuntu 22.04
  • Install and Use ClamAV on Ubuntu 22.04

Alternative Methods for Installing OpenJDK 17 on Ubuntu 22.04

While the above methods provide reliable ways to install OpenJDK 17, let’s explore two alternative approaches.

1. Using SDKMAN! (Software Development Kit Manager)

SDKMAN! is a versatile tool for managing multiple Software Development Kits (SDKs) on Unix-like systems. It simplifies the process of installing, managing, and switching between different versions of Java, Groovy, Gradle, and other SDKs. This method is particularly useful for developers who need to work with multiple Java versions simultaneously.

Installation and Setup:

  1. Install SDKMAN!: Open your terminal and execute the following command:

    curl -s "https://get.sdkman.io" | bash

    Follow the on-screen instructions to complete the installation. You’ll likely need to open a new terminal or source your shell configuration file (e.g., source ~/.bashrc or source ~/.zshrc) for SDKMAN! to be available.

  2. Install OpenJDK 17 with SDKMAN!: Once SDKMAN! is installed, use the following command to list available OpenJDK versions:

    sdk list java

    Identify the appropriate OpenJDK 17 distribution (e.g., java 17.0.x-open) and install it using:

    sdk install java 17.0.x-open

    Replace 17.0.x-open with the exact version number listed by sdk list java.

  3. Set OpenJDK 17 as the Default (Optional): If you want OpenJDK 17 to be your default Java version, use the following command:

    sdk default java 17.0.x-open

Explanation:

SDKMAN! installs OpenJDK 17 in its own managed directory, isolating it from other Java installations on your system. It then modifies your shell environment to point to the selected version when you run java or javac. This avoids potential conflicts and provides a clean way to switch between different Java versions.

Advantages:

  • Version Management: Simplifies managing multiple Java versions.
  • Isolation: Keeps Java installations separate, preventing conflicts.
  • Easy Switching: Quickly switch between different Java versions.

2. Using a Container (Docker)

Containers provide a lightweight and isolated environment for running applications. Docker is a popular containerization platform that allows you to package OpenJDK 17 along with your application into a self-contained image. This method ensures consistent behavior across different environments and simplifies deployment.

Prerequisites:

  • Docker installed on your Ubuntu 22.04 system. Refer to the official Docker documentation for installation instructions.

Steps:

  1. Create a Dockerfile: Create a new file named Dockerfile (without any file extension) in your project directory. Add the following content:

    FROM ubuntu:22.04
    
    # Update the package index and install OpenJDK 17
    RUN apt-get update && apt-get install -y openjdk-17-jdk
    
    # Set the JAVA_HOME environment variable
    ENV JAVA_HOME /usr/lib/jvm/java-17-openjdk-amd64
    
    # Copy your application code into the container
    COPY . /app
    
    # Set the working directory
    WORKDIR /app
    
    # Compile your Java code (if needed)
    RUN javac HelloWorld.java
    
    # Run your Java application
    CMD ["java", "HelloWorld"]
  2. Build the Docker Image: Open your terminal in the directory containing the Dockerfile and execute the following command:

    docker build -t my-java-app .

    This command builds a Docker image named my-java-app based on the instructions in the Dockerfile.

  3. Run the Docker Container: Run the Docker container using the following command:

    docker run my-java-app

Explanation:

The Dockerfile defines the steps to create a Docker image. It starts from an Ubuntu 22.04 base image, installs OpenJDK 17, sets the JAVA_HOME environment variable, copies your application code into the container, compiles the Java code (if necessary), and specifies the command to run your Java application.

Advantages:

  • Consistency: Ensures consistent behavior across different environments.
  • Isolation: Isolates your application and its dependencies from the host system.
  • Portability: Easily deploy your application to different platforms.

These alternative methods offer flexible and powerful ways to Install OpenJDK 17 on Ubuntu 22.04, catering to different development and deployment scenarios. Choose the method that best suits your needs and enjoy developing with Java!