In this tutorial, we’ll explain the steps to upgrade Java 8 to Java 11 on CentOS 7. Java is one of the most popular programming languages in the world, and it is widely used in enterprise applications, web development, and mobile development. If you are using CentOS 7, you might be running an outdated version of Java, such as Java 8, and you might be wondering how to upgrade to Java 11, the latest long-term support (LTS) version of Java. This blog will walk you through the steps to upgrade Java 8 to Java 11 on CentOS 7.
Prerequisites:
- You need ssh access to the CentOS server
- Need sudo privileges or root access
Key features of Java 11:
Java 11 was released in 2018 and is the latest long-term support (LTS) version of Java. It introduced several new features and enhancements to the Java platform. Some of the key features of Java 11 are:
- Local-Variable Syntax for Lambda Parameters: This feature allows you to declare the types of lambda parameters with the ‘var’ keyword, making the code more concise.
- HTTP Client (Standard): The new HTTP client library is now a part of the standard JDK, providing a more modern and efficient way of making HTTP requests.
- Flight Recorder: This feature allows you to collect low-overhead data on your application’s performance and behavior in production environments, without affecting the application’s performance.
- Epsilon: A new experimental garbage collector that does almost no work, making it ideal for applications that generate little to no garbage.
- Nest-Based Access Control: This feature improves the encapsulation of classes by allowing classes to be nested in a way that enforces access control.
- Dynamic Class-File Constants: This feature allows the JVM to load constants directly from the class file, improving performance and reducing memory consumption.
- ZGC (Experimental): A new experimental garbage collector designed for large heaps that can handle very large data sets with very low latency.
- Improved Unicode Support: Java 11 introduces support for Unicode 10.0.0, which includes new characters and scripts.
- TLS 1.3 Support: Java 11 supports the latest version of the Transport Layer Security (TLS) protocol, which provides better security and performance.
- Improved Performance: Java 11 includes various performance improvements, such as reduced startup time and faster execution of applications.
These are just a few of the many features introduced in Java 11. Java 11 also includes various bug fixes, security enhancements, and other improvements to the Java platform.
Steps to Upgrade Java 8 to Java 11 on CentOS 7
Step 1: Command to check which Operating system you are using
$ cat /etc/*release
[root@CentOS-7~]$ cat /etc/*release CentOS Linux release 7.7.1908 (Core) NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7" CentOS Linux release 7.7.1908 (Core) CentOS Linux release 7.7.1908 (Core) [root@CentOS-7~]$
For CentOS 6 – Upgrade Java version from 1.7 to 1.8 on CentOS 6
Step 2: Update the yum package repository:
$ sudo yum update
This will update your system package which includes kernel and dependency packages as well. As shown below screenshot.
Note: Before updating on the production server make sure your application doesn’t have any dependency on the current version because it may impact your application.
Step 3: Check which Java version
[root@CentOS-7 ~]# java -version openjdk version "1.8.0_222-ea" OpenJDK Runtime Environment (build 1.8.0_222-ea-b03) OpenJDK 64-Bit Server VM (build 25.222-b03, mixed mode) [root@CentOS-7 ~]#
So, now we’ll install Java 11 and Upgrade Java 8 to Java 11 on CentOS 7
You can also download the Java package, or else follow the below steps.
Step 4: Command to install JDK 11 in CentOS 7
$ sudo yum install java-11-openjdk-devel
Demo Output:
[root@CentOS-7 ~]# sudo yum install java-11-openjdk-devel Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: centos.mirrors.estointernet.in * extras: centos.mirrors.estointernet.in * updates: centos.excellmedia.net Resolving Dependencies --> Running transaction check ---> Package java-11-openjdk-devel.x86_64 1:11.0.7.10-4.el7_8 will be installed --> Processing Dependency: java-11-openjdk(x86-64) = 1:11.0.7.10-4.el7_8 for package: 1:java-11-openjdk-devel-11.0.7.10-4.el7_8.x86_64 --> Running transaction check ---> Package java-11-openjdk.x86_64 1:11.0.7.10-4.el7_8 will be installed --> Processing Dependency: java-11-openjdk-headless(x86-64) = 1:11.0.7.10-4.el7_8 for package: 1:java-11-openjdk-11.0.7.10-4.el7_8.x86_64 --> Running transaction check ---> Package java-11-openjdk-headless.x86_64 1:11.0.7.10-4.el7_8 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================= Package Arch Version Repository Size ======================================================================================================================================= Installing: java-11-openjdk-devel x86_64 1:11.0.7.10-4.el7_8 updates 3.3 M Installing for dependencies: java-11-openjdk x86_64 1:11.0.7.10-4.el7_8 updates 217 k java-11-openjdk-headless x86_64 1:11.0.7.10-4.el7_8 updates 39 M Transaction Summary ======================================================================================================================================= Install 1 Package (+2 Dependent packages) Total download size: 42 M Installed size: 170 M Is this ok [y/d/N]: y Downloading packages: warning: /var/cache/yum/x86_64/7/updates/packages/java-11-openjdk-11.0.7.10-4.el7_8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Public key for java-11-openjdk-11.0.7.10-4.el7_8.x86_64.rpm is not installed (1/3): java-11-openjdk-11.0.7.10-4.el7_8.x86_64.rpm | 217 kB 00:00:03 (2/3): java-11-openjdk-devel-11.0.7.10-4.el7_8.x86_64.rpm | 3.3 MB 00:00:21 (3/3): java-11-openjdk-headless-11.0.7.10-4.el7_8.x86_64.rpm | 39 MB 00:01:09 --------------------------------------------------------------------------------------------------------------------------------------- Total 620 kB/s | 42 MB 00:01:10 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-7.1908.0.el7.centos.x86_64 (@anaconda) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Is this ok [y/N]: y Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 1:java-11-openjdk-headless-11.0.7.10-4.el7_8.x86_64 1/3 Installing : 1:java-11-openjdk-11.0.7.10-4.el7_8.x86_64 2/3 Installing : 1:java-11-openjdk-devel-11.0.7.10-4.el7_8.x86_64 3/3 Verifying : 1:java-11-openjdk-devel-11.0.7.10-4.el7_8.x86_64 1/3 Verifying : 1:java-11-openjdk-headless-11.0.7.10-4.el7_8.x86_64 2/3 Verifying : 1:java-11-openjdk-11.0.7.10-4.el7_8.x86_64 3/3 Installed: java-11-openjdk-devel.x86_64 1:11.0.7.10-4.el7_8 Dependency Installed: java-11-openjdk.x86_64 1:11.0.7.10-4.el7_8 java-11-openjdk-headless.x86_64 1:11.0.7.10-4.el7_8 Complete! [root@CentOS-7 ~]#
Step 5: Install OpenJDK 11 JRE
sudo yum install java-11-openjdk
JRE is a subset of JDK, and if you already installed the JDK package, you do not need to install this one. You can try with the below command.
Demo output
[root@CentOS-7 ~]# sudo yum install java-11-openjdk Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: centos.mirrors.estointernet.in * extras: centos.mirrors.estointernet.in * updates: centos.excellmedia.net Package 1:java-11-openjdk-11.0.7.10-4.el7_8.x86_64 already installed and latest version Nothing to do [root@CentOS-7 ~]#
Set the default Java 11 version on CentOS 7
We will replace the default Java version from Java 8 with Java 11 version.
This method is to follow when you have multiple Java versions installed on a server, and now you want to set the default java version. This can be done with the alternative system utility.
Step 1: Command to set default Java version
$ sudo alternatives --config java
Just enter the number when prompted which will change the default Java version.
Demo Output:
[root@CentOS-7 ~]# sudo alternatives --config java There are 3 programs which provide 'java'. Selection Command ----------------------------------------------- 1 java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.221-2.6.18.1.el7.x86_64/jre/bin/java) *+ 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b03-1.el7.x86_64/jre/bin/java) 3 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.7.10-4.el7_8.x86_64/bin/java) Enter to keep the current selection[+], or type selection number: 3 [root@CentOS-7 ~]#
Set the default Java Compiler alternatives to use Java 11:
sudo alternatives --config javac
Choose the version corresponding to OpenJDK 11.
Step 2: Verify the Java version
$ java -version
Demo output:
[root@CentOS-7 ~]# java -version openjdk version "11.0.7" 2020-04-14 LTS OpenJDK Runtime Environment 18.9 (build 11.0.7+10-LTS) OpenJDK 64-Bit Server VM 18.9 (build 11.0.7+10-LTS, mixed mode, sharing) [root@CentOS-7 ~]#
How to Set JAVA_HOME Environment Variable
Note: Always ensure that your applications are compatible with the new Java version before making the switch in a production environment.
Step 1: Locate where Java is installed (Java path)
From the below command, you will get the exact Java path
$ sudo update-alternatives --config java
Once you get the above output press the Ctrl+C button so that it will exit. The intention of this command is to get only the path where Java 11 is installed.
From the above output, you can get the path for each Java version that is installed on the system.
#--> path for Java 7 /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.221-2.6.18.1.el7.x86_64/jre/bin/java #--> path for Java 8 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b03-1.el7.x86_64/jre/bin/java #--> path for Java 11 /usr/lib/jvm/java-11-openjdk-11.0.7.10-4.el7_8.x86_64/bin/java
As we have configured Java 11, so copy the path from above.
Step 2: Add Java path at the end of the “~/.bash_profile” file (Environment Variables)
- Add a path at the end of the line in the “~/.bash_profile” file.
vim ~/.bash_profile JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.7.10-4.el7_8.x86_64/bin/java"
Another way to configure JAVA_HOME is by adding the below line in ~/.bash_profile.
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")
Step 3: Verify the bash profile
source ~/.bash_profile echo $JAVA_HOME
Remove Java 8 (Optional)
If you no longer need Java 8, you can remove it:
sudo yum remove java-1.8.0-openjdk
Verify the Java Installation
Ensure that Java 11 is being used:
java -version
This should display OpenJDK 11 information.
Update Java Applications
If you have Java applications, update their configurations to use Java 11.
Test Applications
Test your applications to ensure they are compatible with Java 11.
Conclusion
Upgrading Java 8 to Java 11 on CentOS 7 is a straightforward process that can be completed in just a few steps. By following the steps outlined in this blog, you can upgrade your Java version to the latest LTS version and take advantage of new features and improved performance.
This is the end of the tutorial, Simple Steps to Upgrade Java 8 to Java 11 on CentOS 7.
See also:
How to Upgrade Java 11 To Java 16 on Rocky Linux or AlmaLinux
How to Upgrade Java 8 To Java 11 on Rocky Linux or AlmaLinux
How to Install Java 8 on Rocky Linux 8 / CentOS 8 / RHEL 8: A Step-by-Step Guide
How to Upgrade Java 8 to Java 11 on Ubuntu 20
Steps to Update Java version on CentOS 6
This was very helpful. One issue I found though was with the JAVA_HOME variable. I needed this to work with Gradle project. And Gradle checks that the JAVA_HOME “is valid” and complained about the path as you described it. I had to edit it so it wasn’t just the bin folder. I had to set JAVA_HOME to:
JAVA_HOME=”/usr/lib/jvm/java-11-openjdk-11.0.7.10-4.el7_8.x86_64″
instead of
JAVA_HOME=”/usr/lib/jvm/java-11-openjdk-11.0.7.10-4.el7_8.x86_64/bin/java”
This fixed my gradle issue.