RHEL BasedRocky Linux

How To Install Java on Rocky Linux 10

Install Java on Rocky Linux 10

In this tutorial, we will show you how to install Java on Rocky Linux 10. Rocky Linux stands as a robust, enterprise-grade Linux distribution that serves as an excellent foundation for Java development and deployment. Java remains one of the most popular programming languages worldwide, powering everything from web applications to enterprise systems. Whether you’re setting up a development environment or deploying production applications, having Java properly installed and configured on your Rocky Linux system is essential.

This comprehensive guide covers multiple installation methods, from using the DNF package manager to manual binary installations. You’ll learn the differences between JRE and JDK installations, how to manage multiple Java versions, and essential environment configuration steps. By the end of this tutorial, you’ll have a fully functional Java environment tailored to your specific needs.

Java’s versatility makes it indispensable for modern software development. The OpenJDK implementation provides a free, open-source alternative to Oracle’s commercial offering, making it the preferred choice for most Rocky Linux deployments. Let’s explore the complete installation process step by step.

Prerequisites and System Requirements

Before beginning the Java installation process, ensure your Rocky Linux 10 system meets the necessary requirements. You’ll need a system with sudo privileges and sufficient disk space for the Java installation.

First, verify your system is up to date by running the following commands:

sudo dnf update -y

Check if Java is already installed on your system:

java --version

If you see “command not found,” Java isn’t installed yet. If Java is present, note the version for reference during the installation process.

Your system should have at least 2GB of available disk space and a stable internet connection for downloading packages. Basic command-line knowledge will help you navigate through the installation steps more efficiently.

Understanding the Linux directory structure is beneficial, particularly the /opt, /usr/lib/jvm, and /etc/profile.d/ directories where Java components are typically installed.

Understanding Java Editions and OpenJDK vs Oracle JDK

Java comes in several editions designed for different use cases. Java Platform Standard Edition (SE) serves as the foundation for general application development and is the focus of this guide. Java Enterprise Edition (EE) targets enterprise applications with additional frameworks and APIs, while Java Micro Edition (ME) caters to mobile and embedded devices.

The distinction between JRE and JDK is crucial for your installation decision. The Java Runtime Environment (JRE) provides the necessary components to run compiled Java applications, including the Java Virtual Machine (JVM). The Java Development Kit (JDK) includes the JRE plus additional tools required for developing, compiling, and debugging Java applications.

OpenJDK represents the free, open-source implementation of Java SE, making it the preferred choice for most Linux distributions. Oracle JDK offers commercial support and some proprietary features but requires licensing for production use. OpenJDK and Oracle JDK maintain feature compatibility and similar performance characteristics.

Rocky Linux 10 repositories typically provide OpenJDK versions 8, 11, 17, and 21. Version 11 and 17 are Long Term Support (LTS) releases, making them ideal for production environments requiring stability and extended support cycles.

Method 1: Installing OpenJDK Using DNF Package Manager

The DNF package manager offers the most straightforward approach to installing Java on Rocky Linux 10. This method automatically handles dependencies and integrates seamlessly with the system’s package management.

Installing Java Runtime Environment (JRE)

Begin by updating your package repository to ensure access to the latest available packages:

sudo dnf update -y

Install Java 11 JRE, which is recommended for most applications:

sudo dnf install java-11-openjdk -y

For Java 17 LTS, use:

sudo dnf install java-17-openjdk -y

If you need the latest Java 21 features:

sudo dnf install java-21-openjdk -y

The installation process downloads the package and its dependencies automatically. You’ll see output indicating the packages being installed and their sizes.

Installing Java Development Kit (JDK)

For development purposes, install the complete JDK package. The -devel suffix indicates the development tools are included:

sudo dnf install java-11-openjdk-devel -y

For Java 17 development:

sudo dnf install java-17-openjdk-devel -y

For the latest Java 21 development kit:

sudo dnf install java-21-openjdk-devel -y

Verifying DNF Installation

After installation, verify Java is working correctly:

java -version

You should see output similar to:

openjdk version "11.0.x" 2021-10-19
OpenJDK Runtime Environment (build 11.0.x+y)
OpenJDK 64-Bit Server VM (build 11.0.x+y, mixed mode, sharing)

For JDK installations, verify the compiler is available:

javac -version

The Java installation directory is typically located at /usr/lib/jvm/ where you can find all installed Java versions.

Installing Multiple Java Versions

Rocky Linux allows multiple Java versions to coexist. Install Java 8 alongside newer versions:

sudo dnf install java-1.8.0-openjdk java-1.8.0-openjdk-devel -y

Each version maintains its own directory structure within /usr/lib/jvm/, allowing you to switch between versions as needed.

Method 2: Manual Installation from Binary Archives

Manual installation provides access to the latest Java releases and Oracle’s commercial JDK. This method offers more control but requires manual maintenance and updates.

Downloading Java Binary Archives

Navigate to the official OpenJDK or Oracle JDK download page. For OpenJDK, download the appropriate Linux x64 tarball:

cd /tmp
sudo wget https://download.java.net/java/GA/jdk21/29/GPL/openjdk-21_linux-x64_bin.tar.gz

Verify the download completed successfully by checking the file size and integrity.

Extracting and Installing Binary

Extract the downloaded tarball:

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

Move the extracted directory to the standard Java installation location:

sudo mv jdk-21 /opt/

This places the Java installation in /opt/jdk-21, following Linux filesystem hierarchy standards.

Setting Up Binary Installation

Create symbolic links for system-wide access and integrate with the alternatives system:

sudo alternatives --install "/usr/bin/java" "java" "/opt/jdk-21/bin/java" 0
sudo alternatives --install "/usr/bin/javac" "javac" "/opt/jdk-21/bin/javac" 0

The alternatives system allows easy switching between multiple Java installations.

Advantages and Disadvantages

Manual installation provides immediate access to the latest Java versions and Oracle’s commercial support options. However, you’ll need to manually handle security updates and patches. Package manager installations receive automatic updates through the system’s normal update process, making them more suitable for production environments requiring consistent maintenance.

Configuring Environment Variables

Proper environment variable configuration ensures Java applications can locate the Java installation and associated tools.

Understanding JAVA_HOME

The JAVA_HOME environment variable points to the Java installation directory. Many applications and build tools rely on this variable to locate Java components. Applications like Apache Tomcat, Maven, and Gradle require JAVA_HOME to function correctly.

Find your Java installation path:

readlink -f $(which java)

This command reveals the actual Java executable location.

Temporary Environment Setup

Set environment variables for the current session:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk
export PATH=$PATH:$JAVA_HOME/bin

These settings only apply to the current terminal session and will be lost when you close the terminal.

Permanent Environment Configuration

Create a system-wide profile script for permanent configuration:

sudo tee /etc/profile.d/java.sh <<EOF
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk
export PATH=\$PATH:\$JAVA_HOME/bin
EOF

Make the script executable:

sudo chmod +x /etc/profile.d/java.sh

Apply the changes immediately:

source /etc/profile.d/java.sh

For user-specific configuration, add the exports to ~/.bashrc:

echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk' >> ~/.bashrc
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> ~/.bashrc
source ~/.bashrc

Verifying Environment Setup

Confirm the environment variables are set correctly:

echo $JAVA_HOME
echo $PATH

The output should display your Java installation path and include the Java binary directory in the PATH.

Managing Multiple Java Versions with Alternatives

The Linux alternatives system provides elegant version management for systems with multiple Java installations.

Understanding Linux Alternatives System

The alternatives system creates symbolic links that point to the currently selected version of an application. This allows seamless switching between different Java versions without modifying system configurations or application settings.

Configuring Java Alternatives

List all available Java alternatives:

sudo alternatives --list | grep java

Configure the default Java runtime:

sudo alternatives --config java

This command presents an interactive menu showing all installed Java versions. Select your preferred version by entering the corresponding number.

Similarly, configure the Java compiler:

sudo alternatives --config javac

Setting Default Java Version

The interactive selection process displays available Java installations with priority numbers. Higher priority numbers take precedence when multiple versions are available.

After making your selection, verify the change:

java -version
javac -version

Advanced Alternatives Management

Add custom Java installations to the alternatives system:

sudo alternatives --install "/usr/bin/java" "java" "/opt/custom-jdk/bin/java" 100

Remove alternatives entries:

sudo alternatives --remove java /opt/old-jdk/bin/java

For automated environments, set alternatives non-interactively:

sudo alternatives --set java /usr/lib/jvm/java-11-openjdk/bin/java

Verification and Testing Java Installation

Thorough testing ensures your Java installation functions correctly and meets your application requirements.

Basic Verification Commands

Check the Java runtime version:

java -version

Verify the compiler installation:

javac -version

Display detailed runtime information:

java -XshowSettings:properties -version

Testing with Sample Java Program

Create a simple test program to verify compilation and execution:

cat > HelloWorld.java << 'EOF'
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Java installation successful on Rocky Linux!");
        System.out.println("Java version: " + System.getProperty("java.version"));
        System.out.println("Java vendor: " + System.getProperty("java.vendor"));
    }
}
EOF

Compile the program:

javac HelloWorld.java

Run the compiled program:

java HelloWorld

Successful output confirms both compilation and runtime components are working correctly.

Advanced Testing

Test Java system properties and classpath functionality:

java -cp . -Dproperty.test=value HelloWorld

Verify JVM options and flags:

java -XX:+PrintFlagsFinal -version | head -20

Test with a more complex application or framework to ensure compatibility with your intended use case.

Installation Validation Checklist

Confirm these essential elements:

  • Java runtime accessible from any directory
  • Compiler functioning for development tasks
  • Environment variables properly configured
  • Multiple versions managed through alternatives
  • Test applications compile and run successfully

Troubleshooting Common Java Installation Issues

Common installation problems have straightforward solutions when you know where to look.

Command Not Found Errors

If you encounter “java: command not found” errors, check these areas:

Verify Java installation:

rpm -qa | grep openjdk

Check PATH configuration:

echo $PATH | grep java

Reload shell configuration:

source ~/.bashrc
source /etc/profile

Missing symbolic links often cause command not found errors. Recreate them using the alternatives system.

Version Conflicts

Multiple Java installations can conflict if not properly managed. Use alternatives to resolve conflicts:

sudo alternatives --config java
sudo alternatives --config javac

Verify JAVA_HOME points to the correct installation:

ls -la $JAVA_HOME/bin/java

Package Manager Issues

Repository update problems can prevent Java installation:

sudo dnf clean all
sudo dnf makecache

For dependency conflicts, try installing with additional repository sources:

sudo dnf install epel-release -y
sudo dnf config-manager --set-enabled crb

GPG key validation errors require importing the correct keys:

sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial

Environment Variable Problems

Environment variables not persisting after reboot indicates profile script issues. Verify script permissions and syntax:

ls -la /etc/profile.d/java.sh
cat /etc/profile.d/java.sh

Shell-specific configuration problems may require adding variables to multiple profile files.

Performance and Compatibility

Architecture mismatches cause runtime failures. Verify system architecture:

uname -m
java -version

Memory allocation issues can be resolved by adjusting JVM parameters:

export JAVA_OPTS="-Xmx2g -Xms1g"

Application-specific Java requirements may necessitate specific versions or configurations.

Security Considerations and Best Practices

Maintaining security and following best practices ensures a robust Java environment.

Keeping Java Updated

Security patches are critical for Java installations. Enable automatic updates for package-managed installations:

sudo dnf config-manager --set-enabled auto-updates

Monitor security advisories from Oracle and OpenJDK communities. Subscribe to security mailing lists for timely notifications of critical updates.

Installation Security

Verify download integrity when installing manually:

sha256sum openjdk-21_linux-x64_bin.tar.gz

Use official repositories whenever possible to ensure authentic packages. Third-party repositories may contain modified or outdated versions.

Set appropriate file permissions for Java directories:

sudo chmod -R 755 /usr/lib/jvm
sudo chown -R root:root /usr/lib/jvm

Development Environment Security

Isolate development and production Java versions to prevent conflicts. Use containers or virtual environments for Java development when possible.

Implement proper firewall rules for Java applications:

sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

Best Practices Summary

Regular maintenance includes updating Java installations, monitoring security advisories, and validating application compatibility. Document your Java environment configuration for team members and future maintenance.

Create backup configurations before making changes:

sudo cp /etc/profile.d/java.sh /etc/profile.d/java.sh.backup

Implement monitoring solutions to track Java application performance and resource usage.

Congratulations! You have successfully installed Java. Thanks for using this tutorial for installing the Java programming language on your Rocky Linux 10 system. For additional help or useful information, we recommend you check the official Java website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button