How to install Java on CentOS 7
Install Java on CentOS 7
Objective
Java is one of the most famous programming languages in the world. Its wide adoption over the past years makes it an unavoidable language in the development world. To learn more about the full capabilities of the Java language refer to the official documentation.
How to download and install Java (JDK) on CentOS 7 Linux distribution with OpenJDK:
1. Install the latest JDK.
2. Use curl to download the archive.
3. Extract the files from the archive.
4. Move the extracted folder to the /opt directory.
5. Manually add the Java setup to ‘alternatives’.
6. Verify the Java installation.
Requirements
This tutorial assume that you have a VPS, bare metal server, or as in our case, an OVHcloud Compute Instance running CentOS 7. You should also have basic knoweldge of the command line. If you need help setting up a Public Cloud instance with CentOS 7, follow this guide: Creating and connecting to your first Public Cloud instance.
Instructions
This tutorial uses OpenJDK. This is the open source version but there are many licensed versions provided by a variety of vendors (Oracle, Microsoft, AWS, …).
In this tutorial, you will install OpenJDK, use it, and learn how to switch between different installed versions.
At the time of writing this tutorial, the latest LTS release of Java was 17.x and the last GA release was 18.x.
Installation of the OpenJDK LTS version
Download the latest JDK: JDK18 Download page. Then, use curl to download the archive:
curl -O https://download.java.net/java/GA/jdk18.0.1.1/65ae32619e2f40f3a9af3af1851d6e19/2/GPL/openjdk-18.0.1.1_linux-x64_bin.tar.gz
$ curl -O https://download.java.net/java/GA/jdk18.0.1.1/65ae32619e2f40f3a9af3af1851d6e19/2/GPL/openjdk-18.0.1.1_linux-x64_bin.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 179M 100 179M 0 0 27.6M 0 0:00:06 0:00:06 --:--:-- 29.1M
Extract the files from the archive:
tar xvf openjdk-18.0.1.1_linux-x64_bin.tar.gz
Move the extracted folder to the /opt directory:
sudo mv jdk-18.0.1.1 /opt/
Manually add the java setup to ‘alternatives’. Alternatives creates, removes, maintains, and displays information about the symbolic links comprising the alternatives system.
sudo alternatives --install /usr/bin/java java /opt/jdk-18.0.1.1/bin/java 99 sudo alternatives --install /usr/bin/javac java /opt/jdk-18.0.1.1/bin/javac 99
Now select the desired version of Java you want to use:
sudo update-alternatives --config java
$ sudo update-alternatives --config java Il existe 1 programmes qui fournissent java. Sélection Commande ----------------------------------------------- *+ 1 /opt/jdk-18.0.1.1/bin/java Entrez pour garder la sélection courante [+] ou saisissez le numéro de type de sélection :1
Verify the java installation:
java -version
$ java -version openjdk version "18.0.1.1" 2022-04-22 OpenJDK Runtime Environment (build 18.0.1.1+2-6) OpenJDK 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)
Manage two or more JDK installations
Imagine you have to use another JDK version for a specific project. First, install the specific version of OpenJDK, as described above.
curl -O https://download.java.net/java/early_access/jdk19/21/GPL/openjdk-19-ea+21_linux-x64_bin.tar.gz tar xvf openjdk-19-ea+21_linux-x64_bin.tar.gz sudo mv jdk-19 /opt/
Manually add the new version of java to alternatives:
sudo alternatives --install /usr/bin/java java /opt/jdk-19/bin/java 1 sudo alternatives --install /usr/bin/java javac /opt/jdk-19/bin/javac 1
Select the desired java version:
sudo update-alternatives --config java
$ sudo update-alternatives --config java Il existe 2 programmes qui fournissent « java ». Sélection Commande ----------------------------------------------- *+ 1 /opt/jdk-18.0.1.1/bin/java 2 /opt/jdk-19/bin/java Entrez pour garder la sélection courante [+] ou saisissez le numéro de type de sélection :2
Verify the Java version:
java -version
$ java -version openjdk version "19-ea" 2022-09-20 OpenJDK Runtime Environment (build 19-ea+21-1482) OpenJDK 64-Bit Server VM (build 19-ea+21-1482, mixed mode, sharing)
You can add as many versions as needed by repeating these steps.
If you prefer using the JAVA_HOME environment variable to manage your java version, then create a new file /etc/profile.d/jdk18.0.1.1.sh and include the following:
export JAVA_HOME=/opt/jdk-18.0.1.1 export PATH=$PATH:$JAVA_HOME/bin
Apply the new file and verify the Java version:
$ source /etc/profile.d/jdk18.0.1.1.sh $ echo $JAVA_HOME /opt/jdk-18.0.1.1 $ java -version openjdk version "18.0.1.1" 2022-04-22 OpenJDK Runtime Environment (build 18.0.1.1+2-6) OpenJDK 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)
Test the JDK installation
To test your Java installation, write a Hello World application. Create a HelloWorld.java file and paste the following code into the file:
public class HelloWorld { public static void main(String[] args) { System.out.println("👋 Hello World!"); } }
Save and compile the file, then run it.
javac HelloWorld.java java HelloWorld
The output should look like this:
$ javac HelloWorld.java $ java HelloWorld.java 👋 Hello World!
That’s it, you have successfully installed and configured OpenJDK on CentOS 7.