Home / How To / How to Install Java on CentOS 8

How to Install Java on CentOS 8



Java is a free, open-source and one of the most popular programming languages ​​expressly designed for use in the distributed environment of the Internet. There are two different implementations of Java, Oracle Java and OpenJDK. Oracle Java has some additional commercial features while OpenJDK is an open-source implementation of the Java Platform.

In this tutorial, we will show you how to install OpenJDK 8, OpenJDK 11 and Oracle 8 on CentOS 8.

Requirements

  • A server running CentOS 8.
  • A root password is set up on your system.

Install OpenJDK 11

First, search the OpenJDK available versions in the CentOS 8 repository by running the following command: [1

9659008] dnf search jdk

You should see the following output:

 Last metadata expiration check: 1:23:41 ago on Wednesday, December 18, 2019 02:23:23 AM EST.

================================================== ======= Summary & Name Matched: jdk ===================================== ====================
java-11-openjdk-demo.x86_64: OpenJDK Demos 11
java-1.8.0-openjdk-demo.x86_64: OpenJDK Demos 8
java-11-openjdk-jmods.x86_64: JMods for OpenJDK 11
java-11-openjdk-src.x86_64: OpenJDK Source Bundle 11
java-1.8.0-openjdk-src.x86_64: OpenJDK Source Bundle 8
java-11-openjdk.x86_64: OpenJDK Runtime Environment 11
copy-jdk-configs.noarch: JDKs configuration files copier
java-1.8.0-openjdk.x86_64: OpenJDK Runtime Environment 8
java-11-openjdk-javadoc.x86_64: OpenJDK 11 API documentation
java-1.8.0-openjdk-javadoc.noarch: OpenJDK 8 API documentation
java-11-openjdk-devel.x86_64: OpenJDK Development Environment 11
java-1.8.0-openjdk-devel.x86_64: OpenJDK Development Environment 8
java-11-openjdk-headless.x86_64: OpenJDK Headless Runtime Environment 11
java-1.8.0-openjdk-accessibility.x86_64: OpenJDK 8 accessibility connector
java-1.8.0-openjdk-headless.x86_64: OpenJDK Headless Runtime Environment 8
java-11-openjdk-javadoc-zip.x86_64: OpenJDK 11 API documentation compressed into a single archive
java-1.8.0-openjdk-javadoc-zip.noarch: OpenJDK 8 API documentation compressed into a single archive
================================================== ========== Summary Matched: jdk ==================================== =========================
icedtea-web.noarch: Additional Java components for OpenJDK - Java browser plug-in and Web Start implementation

Now, install OpenJDK 11 using the following command:

 dnf install java-11-openjdk-devel.x86_64 

Once the installation is completed, you can verify the installed Java version by running the following command: [19659008] java version

You should see the following output:

 openjdk version "11.0.5" 2019-10-15 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.5 + 10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.5 + 10-LTS, mixed mode, sharing)

Install OpenJDK 8

Some Java-based applications only support OpenJDK 8. In this case, you will need to install OpenJDK 8 on your server. You can install OpenJDK 8 easily with the following command:

 dnf install java-1.8.0-openjdk-devel 

After installing Java, you can check the installed version of Java with the following command:

 java - version 

You should see the following output:

 openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)

Switch Between Java Versions

You can also install the multiple versions of Java in a single system and switch between them as per your requirements.

You can run the following command to switch the Java version from OpenJDK 8 to OpenJDK 11 as shown below:

 alternatives --config java 

You will be asked to provide your desired Java version as shown below:

 There are 2 programs which provide 'java'.

  Selection Command
-----------------------------------------------
   1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.5.10-0.el8_0.x86_64/bin/java)
* + 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el8_0.x86_64/jre/bin/java)

Enter to keep the current selection [+]or type selection number: 1

Type 1 and hit Enter to switch the Java version.

After change again check the current active Java version with the following command:

 java version 

Install Oracle Java 8

First, you will need to log into the Oracle Java website and go to the Oracle Java 8 JDK Downloads Page. Next, accept the license agreement and download the jdk-8u231-linux-x64.rpm package.

Once the download is completed, run the following command to install Oracle OpenJDK 8:

 dnf localinstall jdk-8u231-linux- x64.rpm 

After installing Oracle OpenJDK 8, you can verify the Java version with the following command:

 java version 

You should see the following output:

 java version "1.8.0_231"
Java (TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot (TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

Conclusion

That's it for now. We learned how to install the different versions of Java in CentOS 8. You can now easily install your desired Java version and switch between them as per your requirements.

 Hitesh Jethva

About Hitesh Jethva

Over 8 years of experience as a Linux system administrator. My skills include in depth knowledge of Redhat / Centos, Ubuntu Nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, Squid proxy, NFS, FTP, DNS, Samba, LDAP, OpenVPN, Haproxy, Amazon web services , WHMCS, OpenStack Cloud, Postfix Mail Server, Security etc.


Source link