CentOSRHEL Based

How To Install Java on CentOS Stream 10

Install Java on CentOS Stream 10

Java is one of the most widely used programming languages in the world, powering a vast array of applications from web servers to mobile apps. For developers and system administrators, knowing how to install Java on CentOS Stream 10 is essential. This guide will walk you through the process step-by-step, ensuring that you have a smooth installation experience.

Prerequisites

System Requirements

Before you begin the installation process, ensure that your system meets the following requirements:

  • A minimum of 1 GB RAM (2 GB recommended).
  • At least 2 GB of free disk space.
  • CentOS Stream 10 installed and updated.

User Privileges

You will need administrative access to install Java. This typically means you should have root privileges or be able to use the sudo command.

Command Line Access

Access your terminal via SSH or directly on your machine. If using SSH, you can connect with:

ssh username@your_server_ip

Updating the System

Keeping your system updated is crucial for security and performance. Before installing Java, run the following command to update your package repository:

sudo dnf update

This command ensures that all your packages are up-to-date, which can help avoid potential conflicts during installation.

Installing OpenJDK

What is OpenJDK?

OpenJDK is an open-source implementation of the Java Platform, Standard Edition. It includes all the necessary tools for developing and running Java applications. OpenJDK is widely used due to its compatibility and community support.

Installation Steps

To install OpenJDK on CentOS Stream 10, follow these steps:

  1. Open your terminal.
  2. Run the following command to install OpenJDK 17 (the latest LTS version):
sudo dnf install java-17-openjdk-devel
  1. Press y when prompted to confirm the installation.

Verifying Installation

After installation, verify that OpenJDK has been installed correctly by checking its version:

java -version

You should see output indicating that Java is installed, including the version number.

Installing Oracle JDK

Difference Between OpenJDK and Oracle JDK

The primary difference between OpenJDK and Oracle JDK lies in licensing and support. While OpenJDK is open-source and free to use, Oracle JDK offers additional features and commercial support but requires a subscription for certain use cases.

Downloading Oracle JDK

If you prefer to use Oracle JDK, follow these steps:

  • Visit the Oracle JDK download page.
  • Select the appropriate version (e.g., JDK 17) for Linux x64.
  • You will need to accept the license agreement before downloading.
  • Download the RPM file to your local machine or directly on your server using wget.

Installation Steps

Once you have downloaded the Oracle JDK RPM file, install it using the following command:

sudo dnf localinstall /path/to/jdk-[version].rpm

This command installs Oracle JDK on your system. Replace /path/to/jdk-[version].rpm with the actual path to your downloaded file.

Verifying Installation

You can verify that Oracle JDK is correctly installed by running:

java -version

The output should confirm that Oracle JDK is installed along with its version number.

Setting JAVA_HOME Environment Variable

Importance of JAVA_HOME

The JAVA_HOME environment variable is crucial for many Java applications as it points to the directory where Java is installed. Setting this variable ensures that applications can locate Java libraries and executables easily.

Finding Java Path

You can find the installation path of Java by executing:

readlink -f $(which java)

This command will return a path similar to /usr/lib/jvm/java-17-openjdk/bin/java. You will need this path for setting JAVA_HOME.

Setting the Environment Variable

Edit your profile file to set JAVA_HOME:

echo 'export JAVA_HOME=/usr/lib/jvm/java-17-openjdk' >> ~/.bash_profile
source ~/.bash_profile

This command adds the JAVA_HOME variable to your .bash_profile and refreshes it so that changes take effect immediately. Make sure to replace /usr/lib/jvm/java-17-openjdk with your actual path if different.

Managing Multiple Java Versions

Using Alternatives Command

If you have multiple versions of Java installed, you can manage them using the alternatives command. This allows you to switch between different installations easily.

Commands to List and Set Default Version

You can list all installed versions of Java with:

sudo alternatives --config java

This command will display a list of installed versions. You can choose which version to set as default by entering the corresponding number from the list.

Troubleshooting Common Issues

Installation Errors

If you encounter errors during installation, here are some common issues and solutions:

  • Error: No package found: Ensure that your package manager is updated by running sudo dnf update.
  • Error: Dependency issues: Check for missing dependencies and resolve them by installing required packages manually.
  • Error: Permission denied: Make sure you are using sudo or have root access when installing packages.

Version Conflicts

If you face conflicts due to multiple versions of Java, use the alternatives command mentioned earlier to set your preferred version as default. This helps avoid confusion when running Java applications.

Congratulations! You have successfully installed Java. Thanks for using this tutorial for installing the Java programming language on CentOS Stream 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