Set up OpenJDK 19 on Ubuntu 22.04: Free Java Platform

Posted on

Set up OpenJDK 19 on Ubuntu 22.04: Free Java Platform

Set up OpenJDK 19 on Ubuntu 22.04: Free Java Platform

In this guide, we will walk you through the process to Set up OpenJDK 19 on Ubuntu 22.04. OpenJDK is a free and open-source Java Platform, Standard Edition implementation. It is a community-driven project that was started in 2006.

OpenJDK is published under the GNU General Public License (GPL), one of the most popular free software licenses. OpenJDK aims to offer a free and high-performance alternative to Oracle JDK.

Ubuntu 22.04 ships with default Java 11. Follow the steps below to install OpenJDK 19 Ubuntu 22.04. Ensuring you can Set up OpenJDK 19 on Ubuntu 22.04 is crucial for developers targeting the latest Java features.

To complete this guide on OpenJDK 19 Ubuntu 22.04, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

Method 1. Installing OpenJDK 19 Ubuntu 22.04

First, you need to update your local package index with the command below:

sudo apt update

Download OpenJDK 19

At this point, you need to visit the JDK Downloads page to download the latest archive by using the wget command:

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

Then, extract your downloaded file with the command below:

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

Next, move your extracted file to the /opt directory:

sudo mv jdk-19.0.2 /opt/

Configure Java Environment Path on Ubuntu 22.04

At this point, you need to configure the Java home path by 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 your profile file with the following command:

source /etc/profile.d/jdk19.sh

Verify your Java Home path:

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

Also, you can verify your Java installation by checking its version:

java -version
Installing OpenJDK 19 Ubuntu 22.04 From Source

Method 2. Install OpenJDK 19 From the Apt Repository

Another way that you can use to install Java 19, is to use the Apt repository on Ubuntu 22.04. This simplifies the process to Set up OpenJDK 19 on Ubuntu 22.04 significantly.

First, you need to update your local package index with the command below:

sudo apt update

Then, use the command below to install Java 19:

apt install openjdk-19-jre-headless -y

Verify your Java installation by checking its version:

java -version
nstall OpenJDK 19 From the Apt Repository

Create a Sample Project with OpenJDK 19

At this point, we will show you how to create a sample project to see that your Java is working correctly on Ubuntu 22.04. Create and open the hello world file with your favorite text editor like nano editor or vi editor:

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");
    }

}

When you are done, save and close the file.

Then, compile and run your Java code:

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

That’s it, you are done.

Conclusion

If you’re a developer or user working with Java applications, OpenJDK 19 on Ubuntu 22.04 provides the tools and features needed to build, run, and optimize modern Java-based software solutions. At this point, you have learned to Set up OpenJDK 19 on Ubuntu 22.04.

Hope you enjoy it. You may also like to read the following articles:

How do I find my OS details in Ubuntu?

How to Install Caddy Webserver on Ubuntu 24.04

How to Install PHP on Ubuntu 24.04

How to Enable or Disable AppArmor on Ubuntu 24.04

Setting a Static IP Address in Ubuntu 24.04 via GUI

Install OpenRGB Ubuntu Linux

View Saved WiFi password in Ubuntu

How to Install Xfce on Ubuntu 22.04

Alternative Methods to Install OpenJDK 19 on Ubuntu 22.04

While the article covers two straightforward methods for installing OpenJDK 19, other approaches exist that can offer flexibility or cater to specific needs. Here are two alternative methods to Set up OpenJDK 19 on Ubuntu 22.04: using SDKMAN! and using a Docker container.

Method 3: Installing OpenJDK 19 using SDKMAN!

SDKMAN! (Software Development Kit Manager) is a tool for managing parallel versions of multiple Software Development Kits on most Unix-like systems. It provides a convenient way to install, switch between, remove and manage different SDKs, including various versions of OpenJDK.

Explanation:

SDKMAN! simplifies the installation and management of multiple Java versions. It handles the download, installation, and environment variable configuration, allowing you to switch between different Java versions effortlessly. This is particularly useful for developers working on projects with different Java version requirements.

Steps:

  1. Install SDKMAN!: Open your terminal and run 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 ~/.sdkman/bin/sdkman-init.sh file for the changes to take effect.

  2. Install OpenJDK 19 using SDKMAN!: Once SDKMAN! is installed, use the following command to install OpenJDK 19:

    sdk install java 19.0.2-open

    This command will download and install OpenJDK 19.0.2 from a suitable vendor (usually, it’s a build from a reputable source like Liberica or BellSoft). You can specify a particular vendor if needed.

  3. Set OpenJDK 19 as the default Java version: To make OpenJDK 19 the default Java version, use the following command:

    sdk use java 19.0.2-open
    sdk default java 19.0.2-open
  4. Verify the installation: Finally, verify that OpenJDK 19 is installed correctly by checking the Java version:

    java -version

    The output should show that you are using OpenJDK 19.

Method 4: Installing OpenJDK 19 using Docker

Docker allows you to run applications in isolated containers, ensuring consistency across different environments. You can use Docker to create a container with OpenJDK 19 pre-installed, providing a portable and reproducible Java development environment.

Explanation:

Using Docker ensures that your Java environment is consistent, regardless of the underlying operating system. This is beneficial for development teams working on different machines or for deploying Java applications to cloud environments.

Steps:

  1. Install Docker: If you don’t have Docker installed, follow the instructions on the official Docker website (https://docs.docker.com/get-docker/) to install Docker Desktop on your Ubuntu 22.04 system.

  2. Create a Dockerfile: Create a new file named Dockerfile in your project directory. Add the following content to the file:

    FROM ubuntu:22.04
    
    # Update the package index and install wget
    RUN apt-get update && apt-get install -y wget
    
    # Download OpenJDK 19
    RUN wget https://download.java.net/java/GA/jdk19.0.2/fdb695a9d9064ad6b064dc6df578380c/7/GPL/openjdk-19.0.2_linux-x64_bin.tar.gz
    
    # Extract OpenJDK 19
    RUN tar xvf openjdk-19.0.2_linux-x64_bin.tar.gz
    
    # Move the extracted directory to /opt
    RUN mv jdk-19.0.2 /opt/
    
    # Set JAVA_HOME environment variable
    ENV JAVA_HOME=/opt/jdk-19.0.2
    
    # Add Java to PATH
    ENV PATH=$PATH:$JAVA_HOME/bin
    
    # Verify Java installation
    RUN java -version
  3. Build the Docker image: In your terminal, navigate to the directory containing the Dockerfile and run the following command to build the Docker image:

    docker build -t openjdk19 .

    This command will download the Ubuntu 22.04 base image, install OpenJDK 19, and configure the environment variables.

  4. Run a Docker container: Once the image is built, you can run a Docker container using the following command:

    docker run -it openjdk19 bash

    This will start a new container and open a bash shell inside the container. You can then run Java commands within the container.

  5. Run a Java Program with Docker:

To run your HelloWorld.java program in the Docker container, you would first need to copy the file into the container. Here’s a modified Dockerfile and the commands to achieve this:

FROM ubuntu:22.04

# Update the package index and install wget
RUN apt-get update && apt-get install -y wget -y

# Download OpenJDK 19
RUN wget https://download.java.net/java/GA/jdk19.0.2/fdb695a9d9064ad6b064dc6df578380c/7/GPL/openjdk-19.0.2_linux-x64_bin.tar.gz

# Extract OpenJDK 19
RUN tar xvf openjdk-19.0.2_linux-x64_bin.tar.gz

# Move the extracted directory to /opt
RUN mv jdk-19.0.2 /opt/

# Set JAVA_HOME environment variable
ENV JAVA_HOME=/opt/jdk-19.0.2

# Add Java to PATH
ENV PATH=$PATH:$JAVA_HOME/bin

# Create a working directory
WORKDIR /app

# Copy the Java source file into the container
COPY HelloWorld.java .

# Compile the Java program
RUN javac HelloWorld.java

# Command to run the Java program
CMD ["java", "HelloWorld"]

Explanation of Changes:

  • WORKDIR /app: Sets the working directory inside the container to /app. This is where our code will reside.
  • COPY HelloWorld.java .: Copies the HelloWorld.java file from your host machine’s current directory into the /app directory within the container.
  • RUN javac HelloWorld.java: Compiles the Java source code within the container.
  • CMD ["java", "HelloWorld"]: Defines the command that will be executed when the container starts. It runs the compiled HelloWorld class.

Building and Running the Docker Image:

  1. Save the Dockerfile: Save the above content to a file named Dockerfile in the same directory as your HelloWorld.java file.
  2. Build the Image: In your terminal, navigate to the directory containing the Dockerfile and HelloWorld.java, then run:

    docker build -t openjdk19-hello .
  3. Run the Container: Run the image:

    docker run openjdk19-hello

    The output will be "Hello, World".

These two alternative methods offer different advantages for managing Java installations on Ubuntu 22.04. SDKMAN! provides a flexible way to manage multiple SDK versions, while Docker ensures consistent and reproducible environments. Choosing the right method depends on your specific needs and preferences. Regardless of the method chosen, ensuring that you can Set up OpenJDK 19 on Ubuntu 22.04 will enable you to take advantage of the latest Java features and improvements.

Leave a Reply

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