Easy Steps To Install OpenJDK 19 on Ubuntu 20.04 – OrcaCore

Posted on

Easy Steps To Install OpenJDK 19 on Ubuntu 20.04 - OrcaCore

Easy Steps To Install OpenJDK 19 on Ubuntu 20.04 – OrcaCore

This guide will walk you through the process of how to Install OpenJDK 19 on Ubuntu 20.04. OpenJDK is an open-source implementation of the Java Standard Edition platform, developed with contributions from Oracle and the broader Java community. Ubuntu 20.04 typically comes with Java 11 as the default. Follow these steps, originally detailed on the Orcacore website, to install OpenJDK 19 on your Ubuntu 20.04 system. Let’s explore how to Install OpenJDK 19 on Ubuntu 20.04.

To successfully complete this guide, ensure you are logged into your server as a non-root user with sudo privileges. If you haven’t already set this up, you can refer to our guide on Initial Server Setup with Ubuntu 20.04.

Method 1. OpenJDK 19 Setup on Ubuntu 20.04

This method involves manually downloading and configuring OpenJDK 19.

Download OpenJDK 19

First, update your local package index to ensure you have the latest package information:

sudo apt update

Now, visit the JDK Downloads page to download the appropriate archive. Use the wget command to download the OpenJDK 19 archive:

sudo wget https://download.java.net/java/GA/jdk19.0.2/fdb695a9d9064ad6b064dc6df578380c/7/GPL/openjdk-19.0.2_linux-x64_bin.tar.gz

Next, extract the downloaded file using the following command:

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

Then, move the extracted directory to the /opt directory:

sudo mv jdk-19.0.2 /opt/

Configure Java Environment Path on Ubuntu 20.04

Configure the Java home path by creating an environment variable using the following command:

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

Source the profile file to apply the changes to your current session:

source /etc/profile.d/jdk19.sh

Verify that the JAVA_HOME environment variable is correctly set:

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

You can also verify the Java installation by checking its version:

java -version
OpenJDK 19 Ubuntu 20

Method 2. Install OpenJDK 19 From the Apt Repository

Another approach to how to Install OpenJDK 19 on Ubuntu 20.04 is using the apt package manager.

First, update your local package index:

sudo apt update

Then, install OpenJDK 19 using the following command:

apt install openjdk-19-jre-headless -y

Verify the Java installation by checking its version:

java -version
OpenJDK 19 Verify on Ubuntu 20

Test OpenJDK Installation on Ubuntu 20.04

Let’s create a simple Java project to confirm that your Java installation is working correctly.

Create and open a file named HelloWorld.java using your preferred text editor. Here, we are using the vi editor:

vi HelloWorld.java

Add the following code 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 the Java code:

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

That’s it! Your OpenJDK 19 installation is working correctly.

Conclusion

You have now successfully learned how to Install OpenJDK 19 on Ubuntu 20.04 using two different methods.

Hope you found this guide helpful! You might also find these articles useful:

  • How To Install Apache Maven on Ubuntu 20.04
  • Install and Configure Jekyll on Ubuntu 20.04
  • Installing Nodejs Ubuntu 20.04
  • LEMP Stack Setup Ubuntu 20.04
  • MariaDB Installation Ubuntu 20.04

Alternative Solutions for Installing OpenJDK 19 on Ubuntu 20.04

While the provided methods are effective, here are two alternative approaches that offer different advantages and considerations.

Alternative 1: Using SDKMAN!

SDKMAN! (Software Development Kit Manager) is a versatile tool for managing multiple software development kits, including different versions of Java. It provides a convenient way to install, switch between, and manage JDKs. This is particularly useful for developers who need to work with multiple Java versions simultaneously.

Explanation:

SDKMAN! simplifies the installation and management of JDKs by handling the download, installation, and environment configuration. It allows you to easily switch between different Java versions using a simple command.

Steps:

  1. Install SDKMAN!

    First, install SDKMAN! using the following command:

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

    Follow the on-screen instructions to complete the installation. You may need to open a new terminal or source your bash profile (source ~/.bashrc or source ~/.zshrc) for the changes to take effect.

  2. Install OpenJDK 19 using SDKMAN!

    Use SDKMAN! to list available OpenJDK versions:

    sdk list java

    Identify the OpenJDK 19 version you want to install (e.g., 19.0.2-open). Install it using the sdk install command:

    sdk install java 19.0.2-open

    SDKMAN! will download and install OpenJDK 19.

  3. Set OpenJDK 19 as the default (Optional):

    If you want to make OpenJDK 19 the default Java version, use the following command:

    sdk default java 19.0.2-open
  4. Verify Installation:

    Check the Java version to confirm the installation:

    java -version

Advantages:

  • Multiple JDK Management: Easily switch between different Java versions.
  • Simplified Installation: SDKMAN! handles the complexities of downloading and configuring JDKs.
  • Clean Uninstallation: Easily remove JDKs using SDKMAN!

Disadvantages:

  • Dependency on SDKMAN!: Requires installing and maintaining SDKMAN!.
  • Slightly Longer Initial Setup: The initial setup involves installing SDKMAN! before installing Java.

Alternative 2: Using a Docker Container

Docker provides a containerization platform that allows you to run applications in isolated environments. You can use a Docker image containing OpenJDK 19 to run Java applications without directly installing Java on your host system.

Explanation:

Docker containers provide a consistent and isolated environment for running applications. By using a Docker image with OpenJDK 19, you can ensure that your Java applications run with the correct Java version, regardless of the Java version installed on the host system.

Steps:

  1. Install Docker:

    If you don’t have Docker installed, install it using the following command:

    sudo apt update
    sudo apt install docker.io

    You may also need to add your user to the docker group to run Docker commands without sudo:

    sudo usermod -aG docker $USER
    newgrp docker

    Log out and log back in for the group change to take effect.

  2. Pull an OpenJDK 19 Docker Image:

    Pull an official OpenJDK 19 Docker image from Docker Hub:

    docker pull openjdk:19
  3. Run a Java Application in a Docker Container:

    Create a directory for your Java application:

    mkdir my-java-app
    cd my-java-app

    Create a HelloWorld.java file with the following content:

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

    Compile the Java code:

    javac HelloWorld.java

    Run the Java application in a Docker container:

    docker run --rm -v "$PWD:/app" -w /app openjdk:19 java HelloWorld

    Explanation of the docker run command:

    • --rm: Automatically removes the container when it exits.
    • -v "$PWD:/app": Mounts the current directory ($PWD) to the /app directory inside the container.
    • -w /app: Sets the working directory inside the container to /app.
    • openjdk:19: Specifies the Docker image to use.
    • java HelloWorld: Executes the java HelloWorld command inside the container.

Advantages:

  • Isolation: Provides an isolated environment for running Java applications.
  • Consistency: Ensures that your application runs with the correct Java version, regardless of the host system.
  • Portability: Easily deploy your application to different environments without worrying about Java version compatibility.

Disadvantages:

  • Overhead: Docker containers have some overhead compared to running applications directly on the host system.
  • Complexity: Requires understanding of Docker concepts and commands.

These alternative solutions offer different approaches to managing and using OpenJDK 19 on Ubuntu 20.04, catering to various needs and preferences. The article has effectively covered how to Install OpenJDK 19 on Ubuntu 20.04.

Leave a Reply

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