Java OpenJDK is a widely used open-source version of Oracle JDK. It provides a platform for developers to write and run Java applications on various operating systems. In this guide, we will walk you through the process of installing Java OpenJDK on AlmaLinux 9 server. We will also cover how to set up the default Java version and configure the JAVA_HOME environment variable.
Prerequisites
Before we begin, make sure you have the following:
- An AlmaLinux 9 server – In this demo, we will be using the AlmaLinux 9 server with the hostname
almalinux9-server
. - A non-root user with sudo/root administrator privileges.
Now, let’s dive into the installation process.
Installing Java OpenJDK
AlmaLinux provides multiple versions of Java OpenJDK in its repository, including OpenJDK 11 and OpenJDK 17. You can install either or both of these versions using the DNF package manager.
Installing Java OpenJDK 11
To install Java OpenJDK 11, open a terminal and run the following command:
sudo dnf install java-11-openjdk java-11-openjdk-devel
When prompted, type y
to confirm the installation and press ENTER
. Once the installation is complete, you can verify the Java version by running the following command:
java --version
You should see an output similar to this:
openjdk version"11.0.13"2021-10-19LTS OpenJDK Runtime Environment18.9(build11.0.13+8-LTS-237) OpenJDK64-Bit ServerVM18.9(build11.0.13+8-LTS-237, mixed mode, sharing)
Installing Java OpenJDK 17
To install Java OpenJDK 17, use the following command:
sudo dnf install java-17-openjdk java-17-openjdk-devel
Again, confirm the installation by typing y
and pressing ENTER
. After the installation is complete, verify the Java version by running:
java --version
The output should indicate that Java OpenJDK 17 is installed:
openjdk version"17"2021-09-14LTS OpenJDK RuntimeEnvironment(build17+35-LTS-2724) OpenJDK64-Bit ServerVM(build17+35-LTS-2724, mixed mode, sharing)
Now that you have installed Java OpenJDK on your AlmaLinux 9 server, let’s move on to setting up the default Java version.
Setting Up Default Java Version
If you have multiple Java versions installed on your system, you can choose the default version that will be used to run your applications. The recommended way to do this is by using the alternatives
command.
To view the available Java versions, run the following command:
sudo alternatives --list | grep java
You will see a list of installed Java versions similar to this:
java-11-openjdk.x86_64 java-17-openjdk.x86_64
To set up the default Java version, run the following command:
sudo alternatives --config java
You will be prompted to enter the number corresponding to your preferred Java version. Select the desired version and press ENTER
to confirm.
Next, verify the Java version by running:
java --version
If the output shows the version you selected, it means that the default Java version has been successfully changed.
In addition to setting up the default Java version, you may also need to change the default javac
version. Use the alternatives
command with the javac
parameter to configure the default javac
version:
sudo alternatives --config javac
Select the desired version and verify the javac
version with:
javac --version
Now that you have set up the default Java version, let’s move on to configuring the JAVA_HOME
environment variable.
Configuring JAVA_HOME Environment Variable
The JAVA_HOME
environment variable is used by various applications, such as Apache Tomcat and Gradle, to locate the Java installation directory. You can configure JAVA_HOME
either system-wide or on a per-user basis.
Setup JAVA_HOME System-wide
To set up JAVA_HOME
system-wide, you can utilize the bash profile configuration in the /etc/profile.d/
directory. This configuration will be loaded automatically at login.
Open a terminal and create a new script called java.sh
in the /etc/profile.d/
directory using the following command:
sudo nano /etc/profile.d/java.sh
In the editor, add the following configuration to set up JAVA_HOME
with Java OpenJDK 17:
exportJAVA_HOME="/usr/lib/jvm/java-17-openjdk"
Save the file and exit the editor. Make the script executable and load it into the current session by running the following commands:
sudo chmod +x /etc/profile.d/java.sh source /etc/profile.d/java.sh
To verify the JAVA_HOME
environment variable, run:
echo $JAVA_HOME
If the operation was successful, you should see the JAVA_HOME
environment variable pointing to the default OpenJDK directory:
/usr/lib/jvm/java-17-openjdk
Setup JAVA_HOME Per-user
Alternatively, you can set up JAVA_HOME
on a per-user basis using the ~/.bashrc
script, which is loaded automatically upon login for each user.
To configure JAVA_HOME
per-user, open a terminal and switch to the desired user using the following command:
su- username
Replace username
with the actual username of the user.
Next, open the ~/.bashrc
file using the command:
nano ~/.bashrc
At the bottom of the file, add the following configuration to set up JAVA_HOME
with Java OpenJDK 17:
exportJAVA_HOME="/usr/lib/jvm/java-17-openjdk"
Save the changes and exit the editor. To apply the changes, reload the ~/.bashrc
file by running:
source ~/.bashrc
Verify the JAVA_HOME
environment variable by running:
echo $JAVA_HOME
If the operation was successful, you should see the JAVA_HOME
environment variable pointing to the default OpenJDK directory.
Congratulations! You have successfully installed Java OpenJDK on your AlmaLinux 9 server and configured the default Java version and JAVA_HOME
environment variable. You can now use Java OpenJDK to develop your Java application projects or deploy Java applications on your system.
Shape.host provides reliable Cloud VPS hosting solutions that can enhance your Java development experience. With Shape.host, you can deploy your Java applications on secure and scalable cloud servers. Explore Shape.host’s Cloud VPS hosting plans today and take your Java projects to the next level.
Conclusion
In this guide, we have covered the step-by-step process of installing Java OpenJDK on AlmaLinux 9. We have explored how to install both Java OpenJDK 11 and Java OpenJDK 17 using the DNF package manager. Additionally, we have learned how to set up the default Java version and configure the JAVA_HOME
environment variable.
Remember to choose the appropriate Java version based on your requirements and preferences. Whether you opt for Java OpenJDK 11 or Java OpenJDK 17, both provide a robust platform for developing and running Java applications.
By following the instructions in this guide, you are now equipped with the knowledge to install and configure Java OpenJDK on your AlmaLinux 9 server. Enjoy coding and developing Java applications with ease and efficiency!