How to install Java on Centos 7 ?
Install Java on Centos 7
Objective
Java is one of the most famous languages in the world. Its wide adoption in the past years makes it an unavoidable language in the development world. To have more information and the capabilities of the Java language see the official documentation.
In this tutorial you will learn how to install a Java Development Kit (JDK) on the Centos 7 Linux distribution.
Requirements
This tutorial assumes that you have a Centos 7, running in an OVHcloud Compute Instance for example, and some basic knowledge of how to operate it. If you don’t have a running Centos 7, follow the guide to use an OVHcloud Compute Instance.
Instructions
This tutorial uses OpenJDK version. This is the open-source version but there are many licensed versions provided by vendors (Oracle, Microsoft, AWS, …).
In this tutorial, you will, first, install a JDK, then you will use it and to finish you learn how to switch between different installed versions.
At the time of writing this tutorial, the last LTS release of Java is 17.x and the last GA release is 18.x.
Installation of the OpenJDK LTS version
Find the link to download the latest JDK on the 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
Test your java install:
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 version for a specific project. First you install the specific version of OpenJDK, the same way you did before:
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
Test if the java link addresses the right 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 you want by repeating these steps.
Now, 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, with this content:
export JAVA_HOME=/opt/jdk-18.0.1.1 export PATH=$PATH:$JAVA_HOME/bin
Apply the new file and verify 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, you can write an Hello World application. Create a HelloWorld.java file and past the following code:
public class HelloWorld { public static void main(String[] args) { System.out.println("👋 Hello World!"); } }
Save and compile it, then, run it:
javac HelloWorld.java java HelloWorld
Output should be like this:
$ javac HelloWorld.java $ java HelloWorld.java 👋 Hello World!
That’s it, you have successfully installed and configured an OpenJDK on Centos 7.
Go further
Check the offers of public cloud instance on OVHcloud.