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 tutorial, you will 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
Minimum of a single Oracle Linux system
Each system should have Oracle Linux installed and configured with:
- A non-root user account with sudo access
- Access to the Internet
Deploy Oracle Linux
Note: If running in your own tenancy, read the linux-virt-labs
GitHub project README.md and complete the prerequisites before deploying the lab environment.
Open a terminal on the Luna Desktop.
Clone the
linux-virt-labs
GitHub project.git clone https://github.com/oracle-devrel/linux-virt-labs.git
Change into the working directory.
cd linux-virt-labs/ol
Install the required collections.
ansible-galaxy collection install -r requirements.yml
Deploy the lab environment.
ansible-playbook create_instance.yml -e localhost_python_interpreter="/usr/bin/python3.6"
The free lab environment requires the extra variable
local_python_interpreter
, which setsansible_python_interpreter
for plays running on localhost. This variable is needed because the environment installs the RPM package for the Oracle Cloud Infrastructure SDK for Python, located under the python3.6 modules.The default deployment shape uses the AMD CPU and Oracle Linux 8. To use an Intel CPU or Oracle Linux 9, add
-e instance_shape="VM.Standard3.Flex"
or-e os_version="9"
to the deployment command.Important: Wait for the playbook to run successfully and reach the pause task. At this stage of the playbook, the installation of Oracle Linux is complete, and the instances are ready. Take note of the previous play, which prints the public and private IP addresses of the nodes it deploys and any other deployment information needed while running the lab.
Check the Availability of Oracle Java
Open a terminal and connect via SSH to the ol-node-01 instance.
ssh oracle@<ip_address_of_instance>
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
andjdk-<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 thejdk-<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
Install one of the Oracle Java versions.
sudo dnf install -y jdk-22-headful
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 that determine default commands. See the update-alternatives(8)
manual page for more details.
Install Oracle Java 17.
sudo dnf install -y jdk-17-headful
Confirm Oracle Java 22 is the default version.
java -version
Change the default to Oracle Java 17.
sudo alternatives --config java
Type
2
and theENTER
key to select Oracle Java 17.Recheck the version.
java -version
Notice the output now shows the version for Oracle Java 17.
Switch the default back to Oracle Java 22.
sudo alternatives --config java
Type
1
and theENTER
key to select Oracle Java 22.
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.
Create the Java program.
tee ~/HelloWorld.java > /dev/null <<'EOF' class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } EOF
Compile the Java program.
javac HelloWorld.java
Run the Java program.
java HelloWorld
Next Steps
That completes the tutorial and demonstrates how to install the Oracle Java SE Platform on Oracle Linux and write your first program.