Read in 4 minutes
How to install Java on CentOS 7
This tutorial shows you how to install Java on CentOS 7. Java is one of the most popular programming languages used to build different kinds of applications and systems.
Java is distributed in three different editions, Standard Edition (SE), Enterprise Edition (EE), and Micro Edition (ME). This tutorial covers the Java SE (Standard Edition) edition.
There are two different Java packages in CentOS 7, Java Runtime Environment (JRE) and the Java Development Kit (JDK). If you only want to run Java programs then you need JRE which contains just the Java Runtime Environment and if you are Java developer then you will need JDK which includes JRE and development/debugging tools and libraries.
There are also two different implementations of Java, OpenJDK and Oracle Java with almost no differences between them except that Oracle Java has a few additional commercial features.
If you are not sure which Java implementation and version to install, the general recommendation is install OpenJDK 8 JDK which is the current LTS version of Java.
OpenJDK, the open source implementation of the Java Platform is the default Java development and runtime in CentOS 7. The installation is simple and straightforward.
Install OpenJDK 8 JDK
At time of writing, OpenJDK 8 is the current LTS version of Java and the recommended version to install. You can install it using yum by typing the following command:
sudo yum install java-1.8.0-openjdk-devel
Install OpenJDK 8 JRE
You can install OpenJDK 8 JRE using yum by typing the following command:
sudo yum install java-1.8.0-openjdk
JRE is a subset JDK and if you already installed the JDK package you do not need to install this one.
Install OpenJDK 7 JDK
If you application requires the older Java SE 7 to be installed on your CentOS 7 server can install OpenJDK 7 JDK package using yum by typing the following command:
sudo yum install java-1.7.0-openjdk-devel
Install OpenJDK 7 JRE
Same as above if you application requires OpenJDK 7 JRE you can install it with yum by typing the following command:
sudo yum install java-1.7.0-openjdk
Install Oracle Java
In this section we will go through the steps of installing Oracle Java. Oracle packages are available to download only from their official website.
We will use
curl to download the required Java package. If you don’t have
curl installed on your system you can install it by typing:
sudo yum install curl
At the time of the writing the latest Oracle Java versions are 8u162 (Java 8) and 9.0.4 (Java 9). If you want to install the latest version head over to the download page, accept the license agreement, copy the download link to the
.rpm package and change it in the
curl command below.
Install Oracle Java 9 JRE
curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/9.0.4+11/c2514751926b4512b076cc82f959763f/jre-9.0.4_linux-x64_bin.rpm sudo yum localinstall jre-9.0.4_linux-x64_bin.rpm
Install Oracle Java 9 JDK
curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/9.0.4+11/c2514751926b4512b076cc82f959763f/jdk-9.0.4_linux-x64_bin.rpm sudo yum localinstall jdk-9.0.4_linux-x64_bin.rpm
Install Oracle Java 8 JRE
curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jre-8u162-linux-x64.rpm sudo yum localinstall jre-8u162-linux-x64.rpm
Install Oracle Java 8 JDK
curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jdk-8u162-linux-x64.rpm sudo yum localinstall jdk-8u162-linux-x64.rpm
Set the default version
We can check the default Java, with:
java version "9.0.4" Java(TM) SE Runtime Environment (build 9.0.4+11) Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
If we have multiple Java versions installed on the server we can change the default version using the
alternatives system utility:
sudo alternatives --config java
The output should look similar to the following:
There are 4 programs which provide 'java'. Selection Command ----------------------------------------------- 1 /usr/java/jdk1.8.0_162/jre/bin/java 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-184.108.40.206-0.b14.el7_4.x86_64/jre/bin/java) 3 java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-220.127.116.11-18.104.22.168.el7_4.x86_64/jre/bin/java) *+ 4 /usr/java/jdk-9.0.4/bin/java Enter to keep the current selection[+], or type selection number:
To change the default Java version, just enter the number when prompted and hit
If for any reason you want to uninstall the Java package, you can unistall it as any other package installed with
yum. For example if you want to uninstall the
java-1.8.0-openjdk-devel package simply run:
sudo yum remove java-1.8.0-openjdk-devel
Now that you have learned how to install and manage different Java versions on your CentOS server, your next step could be to install one of the many applications which runs on Java, such as Tomcat, JBoss/WildFly, Jetty, Glassfish, WebLogic, Cassandra, Jenkins ..etc
If you have any questions feel free to leave a comment.