Install Oracle Java SE on Oracle Linux

3
0
Send lab feedback

Install Oracle Java SE on Oracle Linux

Introduction

Oracle's Java Platform , Standard Edition (Java SE ) lets you develop and deploy Java applications on desktops and servers.

The Java Platform Standard Edition Development Kit (JDK) includes tools for developing, testing, and monitoring programs written in the Java programming language and running on the Java platform. The JDK offers developers and users a rich user interface and the performance, versatility, portability, and security that today's applications require.

Beyond these great features, Oracle provides the Oracle JDK free for commercial and production use. When you combine Oracle Java with Oracle's highly performant, reliable, and cloud-ready operating system, Oracle Linux , you get an ideal environment with out-of-the-box optimizations for Oracle software, excellent for running Oracle Java.

Objectives

In this lab, you'll learn how to:

  • Check for available Oracle Java RPMs
  • Install Oracle Java
  • Verify the JDK version
  • Set the default JDK on a system with multiple versions
  • Run a basic Java "Hello World" program

Prerequisites

  • A running instance of Oracle Linux

Verify the Lab Environment

Note: When using the free lab environment, see Oracle Linux Lab Basics for connection and other usage instructions.

  1. Open a terminal and connect via ssh to the ol-node01 instance.

    ssh oracle@<ip_address_of_ol_node01>
  2. Check the availability of the Oracle Java packages.

    sudo dnf list jdk*

    When using Oracle Linux in Oracle Cloud Infrastructure (OCI), the Oracle Java packages exist in the ol<version>_oci_included repositories. OCI installs and enables these repositories by default when provisioning an Oracle Linux instance.

    Note: Oracle splits the JDK installation on Oracle Linux into two packages: jdk-<version>-headless and jdk-<version>-headful.

    • jdk-<version>-headless is a headless Java Runtime for running non-GUI applications.

    • jdk-<version>-headful is headful Java Runtime and a set of Development Tools for developing and running all types of applications.

    The jdk-<version>-headful package requires the jdk-<version>-headless package. When installing these packages together, they provide the same functionality as the JDK package Oracle provides for Generic Linux Platforms. See the Installation Guide in the Oracle JDK documentation for more details.

Install Oracle Java

  1. Install one of the Oracle Java versions.

    sudo dnf install -y jdk-21-headful
  2. Confirm the installation by checking the JDK version.

    java -version

    Note: To help ensure that your systems are secure when using Java-based content, Oracle strongly recommends that all Java SE users upgrade to the latest release available for the version you are using.

(Optional) Setting the default JDK on a system with multiple versions

If multiple versions of the JDK exist on an Oracle Linux system, users can set the default version using the alternatives command, which maintains symbolic links determining default commands. See the update-alternatives(8) manual page for more details.

  1. Install Oracle Java 17.

    sudo dnf install -y jdk-17-headful
  2. Confirm Oracle Java 21 is the default version.

    java -version
  3. Change the default to Oracle Java 17.

    sudo alternatives --config java

    Type 2 and the ENTER key to select Oracle Java 17.

  4. Recheck the version.

    java -version

    Notice the output now shows the version for Oracle Java 17.

  5. Switch the default back to Oracle Java 21.

    sudo alternatives --config java

    Type 1 and the ENTER key to select Oracle Java 21.

Write, Compile and Run a Java Program

A "Hello, World!" program outputs Hello, World! or a similar welcome message on the screen. The syntax creates a straightforward program and is commonly used to introduce a new programming language to new developers.

  1. Create the Java program.

    tee ~/HelloWorld.java > /dev/null <<'EOF'
    class HelloWorld {
      public static void main(String[] args) {
        System.out.println("Hello, World!"); 
      }
    }
    EOF
  2. Compile the Java program.

    javac HelloWorld.java
  3. Run the Java program.

    java HelloWorld

Summary

That completes the lab and demonstrates how to install the Oracle Java SE Platform on Oracle Linux and write your first program.

For more information

2024-05-15T10:07:24.852Z