How to Install Java on Rocky Linux

Install Java on Rocky Linux

Are you ready to set up Java on your Rocky Linux system? Whether you’re a seasoned developer or just starting your programming journey, installing Java on Rocky Linux doesn’t have to be complicated. In this comprehensive guide, I’ll walk you through multiple methods to get Java up and running on your Rocky Linux machine, from the simplest package manager approach to advanced multi-version management techniques.

Rocky Linux has become increasingly popular among developers and system administrators since its release in 2021, with over 100,000 downloads in its first month alone. As a community-driven, enterprise-grade Linux distribution, it provides an excellent foundation for Java development environments.

What is Rocky Linux and Why Use It for Java Development?

Rocky Linux is a community-driven enterprise Linux distribution that serves as a downstream, complete binary-compatible release using Red Hat Enterprise Linux (RHEL) source code. Created by Gregory Kurtzer, the founder of CentOS, Rocky Linux emerged as a reliable alternative when CentOS shifted its focus to CentOS Stream.

For Java developers, Rocky Linux offers several compelling advantages. First, its enterprise-grade stability ensures your Java applications run reliably in production environments. The distribution receives regular security updates and maintains long-term support, making it ideal for enterprise Java deployments that need to run for years without major disruptions.

Additionally, Rocky Linux’s RHEL compatibility means it benefits from extensive testing and validation that enterprise environments demand. This compatibility extends to Java runtime environments, ensuring optimal performance and stability for your Java applications.

Understanding Java Versions and Distributions

Before diving into installation methods, let’s clarify the different Java options available. Understanding these differences will help you make informed decisions about which Java version and distribution best suits your needs.

Oracle JDK vs OpenJDK – Making the Right Choice

The Java ecosystem offers two primary distributions: Oracle JDK and OpenJDK. OpenJDK is the open-source reference implementation of Java, while Oracle JDK is Oracle’s commercial distribution based on OpenJDK with additional features and support.

OpenJDK has become the preferred choice for most developers and organizations. It’s completely free, open-source, and receives regular updates from the global Java community. Major Linux distributions, including Rocky Linux, provide OpenJDK packages through their official repositories, making installation and updates seamless.

Oracle JDK, on the other hand, requires a commercial license for production use since Java 11. While it includes some additional tools and features, the performance differences between Oracle JDK and OpenJDK have become negligible in recent years.

Java 8, 11, 17, and 21 – Which Version Should You Install?

Java follows a predictable release cycle, with new versions arriving every six months. However, certain versions receive Long Term Support (LTS) designation, making them more suitable for production environments.

Java 8 remains widely used despite being released in 2014. Many legacy applications still depend on Java 8, and it continues to receive security updates. However, for new projects, consider more recent LTS versions.

Java 11, released in 2018, was the first LTS version after Java 8. It introduced several performance improvements and new features while maintaining good backward compatibility. Many organizations have standardized on Java 11 for their production environments.

Java 17, released in 2021, is the current recommended LTS version. It includes significant performance enhancements, new language features, and improved security. If you’re starting a new project, Java 17 is an excellent choice.

Java 21, the latest LTS version released in 2023, introduces virtual threads, pattern matching enhancements, and other modern features. While cutting-edge, it may not yet have the widespread adoption of Java 17.

Prerequisites Before Installing Java on Rocky Linux

System Requirements and Compatibility

Before installing Java on Rocky Linux, ensure your system meets the minimum requirements. Java 8 requires at least 128 MB of RAM, though 512 MB is recommended for development work. For Java 11 and later versions, consider having at least 1 GB of RAM available.

Your Rocky Linux system should have sufficient disk space – typically 200-300 MB for a basic Java installation, though development environments with IDEs and additional tools may require several gigabytes.

Check your system architecture using the uname -m command. Most modern systems use x86_64 architecture, which is well-supported by all Java distributions.

User Permissions and Sudo Access

Installing Java on Rocky Linux typically requires administrative privileges. Ensure your user account has sudo access or you have the root password available. You can verify sudo access by running sudo whoami, which should return “root” if configured correctly.

For development environments, consider creating a dedicated user account for Java development. This practice improves security and makes it easier to manage Java-related configurations without affecting system-wide settings.

Method 1: Installing Java Using DNF Package Manager

The DNF package manager provides the simplest and most reliable method to install Java on Rocky Linux. This approach automatically handles dependencies, security updates, and system integration.

Installing OpenJDK 8 on Rocky Linux

OpenJDK 8 remains popular for maintaining legacy applications. To install OpenJDK 8, first update your package repository:

sudo dnf update -y

Then install OpenJDK 8 development kit:

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

The java-1.8.0-openjdk package provides the runtime environment, while java-1.8.0-openjdk-devel includes development tools like javac compiler and other utilities essential for Java development.

Installing OpenJDK 11 on Rocky Linux

Java 11 offers an excellent balance between modern features and stability. Install OpenJDK 11 using:

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

This installation includes the Java Runtime Environment (JRE) and Java Development Kit (JDK) components. The devel package is crucial if you plan to compile Java applications or use development tools.

Installing OpenJDK 17 on Rocky Linux

For new projects, OpenJDK 17 provides the latest LTS features and performance improvements:

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

OpenJDK 17 includes significant improvements in garbage collection, security enhancements, and new language features that can improve your application’s performance and developer productivity.

Installing OpenJDK 21 on Rocky Linux

To install the newest LTS version with cutting-edge features:

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

Java 21 introduces virtual threads (Project Loom), pattern matching improvements, and other modern features that can significantly impact application performance and development experience.

Method 2: Installing Oracle JDK Manually

While OpenJDK meets most development needs, some scenarios require Oracle JDK. This method involves downloading and installing Oracle’s official distribution.

Downloading Oracle JDK from Official Website

Visit Oracle’s official Java download page and select your preferred JDK version. You’ll need to create an Oracle account and accept the license agreement. Download the RPM package for Linux x64 systems.

For automation purposes, you can download Oracle JDK using wget, though you’ll need to handle authentication cookies:

wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" \
"https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm"

Installing Oracle JDK from RPM Package

Once downloaded, install the Oracle JDK RPM package:

sudo dnf localinstall jdk-17_linux-x64_bin.rpm -y

This command installs Oracle JDK and automatically resolves any dependencies. The installation typically places Oracle JDK in /usr/java/ directory.

Method 3: Installing Java Using SDKMAN

SDKMAN (Software Development Kit Manager) provides an excellent solution for managing multiple Java versions and other development tools.

Setting Up SDKMAN on Rocky Linux

First, install curl if it’s not already available:

sudo dnf install curl unzip zip -y

Install SDKMAN using their installation script:

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

Verify SDKMAN installation:

sdk version

Managing Multiple Java Versions with SDKMAN

List available Java versions:

sdk list java

Install specific Java versions:

sdk install java 17.0.7-tem
sdk install java 11.0.19-tem
sdk install java 8.0.372-tem

Set default Java version:

sdk default java 17.0.7-tem

Switch between versions for current session:

sdk use java 11.0.19-tem

Verifying Your Java Installation

After installation, verify Java is working correctly. Check the Java runtime version:

java -version

For development environments, also verify the compiler:

javac -version

Both commands should return version information matching your installed Java distribution. If you see “command not found” errors, check your PATH environment variable configuration.

Create a simple test program to ensure everything works:

echo 'public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, Rocky Linux!"); } }' > HelloWorld.java
javac HelloWorld.java
java HelloWorld

If you see “Hello, Rocky Linux!” output, your Java installation is working correctly.

Configuring Java Environment Variables

Proper environment variable configuration ensures Java applications and development tools work correctly across your system.

Setting JAVA_HOME Environment Variable

JAVA_HOME points to your Java installation directory. Many applications and build tools rely on this variable. For OpenJDK installations via DNF, determine the installation path:

sudo alternatives --config java

Typically, OpenJDK installs in /usr/lib/jvm/ directory. Set JAVA_HOME permanently by editing your shell profile:

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

For system-wide configuration, create a script in /etc/profile.d/:

sudo tee /etc/profile.d/java.sh > /dev/null <

Updating PATH Variable for Java

The PATH variable ensures Java executables are accessible from any directory. If you installed Java manually or using SDKMAN, you might need to update PATH manually:

export PATH=$PATH:$JAVA_HOME/bin

Make this change permanent by adding it to your shell profile as shown above.

Managing Multiple Java Versions on Rocky Linux

Development environments often require multiple Java versions for different projects or compatibility testing.

Using the alternatives Command

Rocky Linux’s alternatives system provides an elegant way to manage multiple Java installations:

sudo alternatives --install /usr/bin/java java /usr/lib/jvm/java-8-openjdk/bin/java 1
sudo alternatives --install /usr/bin/java java /usr/lib/jvm/java-11-openjdk/bin/java 2
sudo alternatives --install /usr/bin/java java /usr/lib/jvm/java-17-openjdk/bin/java 3

Configure which version to use:

sudo alternatives --config java

This command presents a menu allowing you to select the default Java version.

Switching Between Different Java Versions

For project-specific Java versions, create shell scripts or use environment modules. A simple approach involves creating wrapper scripts:

#!/bin/bash
# java8.sh
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk
export PATH=$JAVA_HOME/bin:$PATH
exec "$@"

Use the wrapper:

./java8.sh java -version

Common Installation Issues and Troubleshooting

Even with careful preparation, installation issues can occur. Here are solutions to common problems.

Resolving Permission Denied Errors

Permission errors typically indicate insufficient privileges. Ensure you’re using sudo for system-wide installations:

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

For SDKMAN installations, avoid using sudo as it installs in your home directory.

Fixing Package Not Found Issues

If DNF cannot find Java packages, update your repository metadata:

sudo dnf clean all
sudo dnf makecache
sudo dnf update -y

Verify the EPEL repository is enabled for additional packages:

sudo dnf install epel-release -y

Environment Variable Configuration Problems

Environment variable issues often stem from incorrect paths or shell configuration problems. Verify your JAVA_HOME setting:

echo $JAVA_HOME
ls -la $JAVA_HOME/bin/java

If JAVA_HOME is empty or points to a non-existent directory, reconfigure it following the steps in the environment variables section.

Best Practices for Running Java on Rocky Linux

Implementing best practices ensures optimal performance and maintainability of your Java environment.

Keep your Java installation updated by regularly running system updates:

sudo dnf update -y

For production environments, consider using specific Java versions rather than always upgrading to the latest release. This approach provides stability and predictable behavior.

Monitor Java application performance using built-in tools like JConsole or VisualVM. These tools help identify memory leaks, performance bottlenecks, and other issues.

Implement proper logging configuration for Java applications. Rocky Linux’s systemd integration makes it easy to manage Java application logs centrally.

Consider using containerization technologies like Docker or Podman for Java applications. Rocky Linux provides excellent support for container technologies, making deployment and scaling easier.

Frequently Asked Questions

Q1: Can I install multiple Java versions simultaneously on Rocky Linux?

Yes, absolutely! Rocky Linux supports multiple Java versions concurrently. You can use the alternatives system to manage different versions, or tools like SDKMAN for more advanced version management. Each installation occupies separate directories, allowing you to switch between versions as needed for different projects.

Q2: What’s the difference between OpenJDK and Oracle JDK for Rocky Linux users?

OpenJDK is completely free and open-source, with excellent performance and regular updates. Oracle JDK requires commercial licensing for production use since Java 11 but includes additional tools and commercial support. For most Rocky Linux users, OpenJDK provides everything needed for development and production deployments.

Q3: How do I uninstall Java from Rocky Linux if I no longer need it?

To remove OpenJDK installed via DNF, use: sudo dnf remove java-*-openjdk java-*-openjdk-devel. For Oracle JDK installed from RPM, use: sudo dnf remove jdk-*. If you used SDKMAN, run: sdk uninstall java <version>. Remember to clean up environment variables in your shell profile files.

Q4: Why am I getting “java: command not found” after installation?

This typically indicates PATH configuration issues. Verify Java is installed with ls /usr/lib/jvm/, then check your PATH variable with echo $PATH. Ensure it includes your Java installation’s bin directory, and source your shell profile with source ~/.bashrc to apply changes.

Q5: Which Java version should I choose for new projects on Rocky Linux?

For new projects, Java 17 is recommended as it’s the current LTS version with modern features and excellent performance. Java 21 offers cutting-edge features if you need the latest capabilities. Avoid Java 8 for new projects unless you have specific compatibility requirements, as newer versions provide significant improvements in security and performance.

Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts