Java is the most popular programming language, use to build various type of applications like a desktop application, web application, mobile application, etc.
The Java comes in two different packages, one is Java Runtime Environment (JRE), and another one is Java Development Kit (JDK).
When we need to run a Java-based application or java program, we need only Java Runtime Environment, but if you want to develop Java application, you need to Java Development Kit (JDK). JDK includes both JRE and debugging tools and development libraries.
Java also has two different flavors, one is Open JDK, and another one is Oracle Java, both have the same functionalities and capabilities, the only difference is Oracle Java has some more commercial features. The default Java flavor of Linux Operating System is OpenJDK.
If you are a little bit confuse to choose between OpenJDK and Oracle Java, you can stick with default OpenJDK available on CentOS 6.
In this tutorial article, we will walk through the process to install Java on CentOS 6 machine.
Before continuing this tutorial, make sure you have CentOS 6 installed machine and a user to login into the system with sudo privileges.
Install OpenJDK 8 JDK
OpenJDK 8 is the most widely-used version of Java. If your application required old version 8 of Java, you could install it by using below command.
$ sudo yum install java-1.8.0-openjdk-devel
Install OpenJDK 8 JRE
If you only want to run a java application, you can install OpenJDK 8 JRE (Java Runtime Environment) by using the following command:
$ sudo yum install java-1.8.0-openjdk
You can verify java installation by checking the version of installed Java, as shown below:
$ java –version
If OpenJDK installed you will get the output like below image:
Now OpenJDK 8 is installed successfully into your CentOS 6 machine.
Install OpenJDK 11 JDK
OpenJDK 11 is the latest version of Java; you can install it in your CentOS 6 machine by using the following command:
$ sudo yum install java-11-openjdk-devel
You can verify OpenJDK 11 installation by checking the version of installed java, as shown below:
$ java –version
If OpenJDK installed you will get output like below:
Install OpenJDK 11 JRE
To run the java application with the latest Java Runtime Environment, you should install OpenJDK 11 JRE by using the following command:
$ sudo yum install java-11-openjdk
Oracle Java Installation
First, you should be aware of the Oracle Java Licence; it is free only for non-commercial use of the software like development and personal use.
You can download Oracle Java from the official website. You should download Oracle Java .rpm packages from the Java SE Downloads page. To download Oracle Java, you have to register on the oracle site.
After downloading .rpm package for Oracle Java, You can install it on CentOS 6 by using the following command:
$ sudo yum localinstall jre-8u221-linux-x64.rpm
To execute above command first need to download java from official website and use the same rpm file in localinstall command as shown above.
Java default version Setup
If you have installed multiple Java into your CentOS 6 machine, you can check the default version of Java by checking the version, as shown below:
$ java -version
The output of the above command will look like below image:
You can change the default Java version by using update-alternatives command, as shown below:
$ sudo alternatives --config java
You will get the list of all installed Java version on your CentOS 6 machine, Enter the number of the version you want to use as a default and press Enter button.
If you want to remove Java package from your CentOS system, you can uninstall it like any other application with yum package manager.
To uninstall the java-1.8.0-openjdk-devel package from your CentOS machine, you can use below command:
$ sudo yum removejava-1.8.0-openjdk-devel
Now you have learned how to install Java on CentOS 6 operating system. You get java installation instruction for both OpenJDK and Oracle Java.