Install OpenJDK 19 on Centos 7: Free JDK – OrcaCore

Posted on

Install OpenJDK 19 on Centos 7: Free JDK - OrcaCore

Install OpenJDK 19 on Centos 7: Free JDK – OrcaCore

This tutorial provides a comprehensive guide on how to Install OpenJDK 19 on Centos 7. OpenJDK, the Open Java Development Kit, represents a free and open-source implementation of the Java Development Kit (JDK) for the Java Platform, Standard Edition (Java SE). Born from an initiative by Sun Microsystems in 2006 and currently under the sponsorship and leadership of Oracle, OpenJDK is distributed under the GNU General Public License (GNU GPL) version 2 with a linking exception. This exception is crucial; without it, any components linked to the Java class library would inherit the restrictive terms of the GPL license.

Follow the steps outlined below to successfully Install OpenJDK 19 on Centos 7, brought to you by OrcaCore.

Before proceeding, ensure you are logged into your Centos 7 server as a non-root user with sudo privileges. If needed, refer to our guide on Initial Server Setup with Centos 7 for assistance.

Installing OpenJDK 19 on Centos 7

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

sudo yum update -y

Next, install the necessary packages for OpenJDK 19 on Centos 7 by executing the command below:

sudo yum install curl wget -y

Download OpenJDK 19

Navigate to the JDK Downloads page to obtain the latest archive. Use the wget command to download it:

sudo wget https://download.java.net/java/GA/jdk19.0.1/afdd2e245b014143b62ccb916125e3ce/10/GPL/openjdk-19.0.1_linux-x64_bin.tar.gz

After downloading, extract the archive:

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

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

sudo mv jdk-19.0.1 /opt/

Configure Java Environment Path

Configure the Java home path by creating a shell script in /etc/profile.d/:

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

Source the profile file to apply the changes:

source /etc/profile.d/jdk19.sh

Verify the Java Home path:

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

Confirm the Java installation by checking its version:

java -version
Install OpenJDK 19 on Centos 7

Install Java SE Development Kit 19 on Centos 7

Alternatively, you can opt for the official Java SE Development Kit 19. Download the RPM package for CentOS / RHEL / Fedora systems:

sudo wget https://download.oracle.com/java/19/latest/jdk-19_linux-x64_bin.rpm

Install the RPM package using the yum or rpm command:

sudo rpm -Uvh jdk-19_linux-x64_bin.rpm
**Output**
warning: jdk-19_linux-x64_bin.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk-19-2000:19.0.2-7             ################################# [100%]

Verify the Java installation on Centos 7:

java -version
**Output**
openjdk version "19.0.1" 2022-10-18
OpenJDK Runtime Environment (build 19.0.1+10-21)
OpenJDK 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)

Configure the Java environment:

cat <<EOF | sudo tee /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/java/default
export PATH=$PATH:$JAVA_HOME/bin
EOF

Source the file to apply the changes:

source /etc/profile.d/jdk.sh

Create a sample project with Java 19

To confirm that Java 19 is working correctly on Centos 7, create a sample project.

Create and open a "Hello, World" file using a text editor (e.g., vi):

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

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

Success!

Conclusion

You have now successfully learned how to Install OpenJDK 19 on Centos 7. OpenJDK provides a free and readily available JDK for your Java development projects.

Enjoy! You might also find these guides helpful:

Install Git on Centos 7

Enable RPM Fusion Repository on Centos 7

Add Swap on Centos 7

Alternative Solutions for Installing OpenJDK 19 on Centos 7

While the above methods detailed how to manually download and install OpenJDK 19, there exist alternative, often simpler, methods for achieving the same goal. These methods leverage package managers and containerization to streamline the process.

1. Using sdkman! (Software Development Kit Manager)

sdkman! is a versatile tool designed to manage multiple software development kits (SDKs) on Unix-like systems. It simplifies the installation, switching, and management of different JDK versions, including OpenJDK. This approach offers a clean and organized way to handle Java installations.

Steps:

  1. Install sdkman!:

    curl -s "https://get.sdkman.io" | bash
    source "$HOME/.sdkman/bin/sdkman-init.sh"

    This command downloads and executes the sdkman! installation script. After installation, you need to source the sdkman-init.sh file to initialize the sdkman! environment in your current shell.

  2. List Available OpenJDK Versions:

    sdk list java

    This command displays a list of available Java versions, including various OpenJDK distributions.

  3. Install OpenJDK 19:

    sdk install java 19.0.1-open

    Replace 19.0.1-open with the appropriate identifier from the sdk list java output if a different version or distribution is desired. sdkman! will automatically download, install, and configure the chosen OpenJDK version.

  4. Set OpenJDK 19 as Default:

    sdk default java 19.0.1-open

    This sets OpenJDK 19 as the default Java version to be used in your shell.

  5. Verify Installation:

    java -version

    Confirm that the installed OpenJDK 19 is correctly configured.

Advantages:

  • Simplified installation and management of multiple JDK versions.
  • Easy switching between different JDKs.
  • Clean and organized installation directory.

Disadvantages:

  • Requires installing sdkman! itself.
  • Adds a dependency on a third-party tool.

2. Using Docker

Docker provides a containerization platform that allows you to run applications in isolated environments. Using Docker to install OpenJDK 19 offers a portable and reproducible way to manage your Java environment, eliminating potential conflicts with other system configurations.

Steps:

  1. Install Docker:

    Follow the official Docker documentation to install Docker on your Centos 7 system: https://docs.docker.com/engine/install/centos/

  2. Pull an OpenJDK 19 Docker Image:

    docker pull openjdk:19

    This command downloads the official OpenJDK 19 Docker image from Docker Hub. Alternatively, you could use a specific distribution like amazoncorretto:19 or eclipse-temurin:19.

  3. Run a Container with OpenJDK 19:

    docker run -it openjdk:19 bash

    This command starts an interactive container based on the OpenJDK 19 image, providing you with a bash shell inside the container. You can now run Java commands within this isolated environment.

  4. Verify Installation (inside the container):

    java -version

    Confirm that OpenJDK 19 is correctly installed and configured inside the Docker container.

  5. Mount a Volume (Optional):

    To work with your local files inside the container, you can mount a volume:

    docker run -it -v /path/to/your/project:/app openjdk:19 bash

    This mounts your local project directory (/path/to/your/project) to the /app directory inside the container. You can then compile and run your Java code from within the container using the mounted volume.

Advantages:

  • Isolated Java environment, preventing conflicts with other system configurations.
  • Portable and reproducible environment across different systems.
  • Simplified dependency management.

Disadvantages:

  • Requires installing and configuring Docker.
  • Adds a layer of abstraction with containerization.
  • Can be more resource-intensive than native installation.

Example Dockerfile:

For a more streamlined and repeatable approach, you can create a Dockerfile:

FROM openjdk:19

# Set the working directory
WORKDIR /app

# Copy the Java source code
COPY HelloWorld.java .

# Compile the Java code
RUN javac HelloWorld.java

# Run the Java application
CMD ["java", "HelloWorld"]

To build and run this Dockerfile:

docker build -t my-java-app .
docker run my-java-app

This Dockerfile automates the process of copying, compiling, and running your HelloWorld.java application within the OpenJDK 19 container.

These alternative methods provide different approaches to installing and managing OpenJDK 19 on Centos 7, offering flexibility and options based on your specific needs and preferences. Whether you choose sdkman! for its simplicity in managing multiple SDKs or Docker for its isolated and portable environments, these alternatives can significantly streamline your Java development workflow.

Leave a Reply

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