Thursday, July 21, 2022

 

Install Java 17 (OpenJDK 17) on Rocky Linux 8|CentOS 8/7

Java is a high-level object-oriented programming language and computing platform intended to let application developers write once and run everywhere. This means that, a compiled java code runs on all platforms that support Java without need for recompilation. JDK is a collection of various programming tools such as JRE(Java Runtime Environment), Java, Javac, Jar and many others. In this guide, we will walk through how to install Java 17 (OpenJDK 17) on Rocky Linux 8|CentOS 8|CentOS 7

Java 17 LTS is the latest long-term support release for the Java SE platform released on 14 September, 2021. It has the following features.

  • New rendering pipeline for MacOS, using the Apple Metal API as an alternative to the existing pipeline that uses the deprecated OpenGL API
  • Porting the JDK to MacOS/AArch64 in response to Apple’s plan to transition its Macintosh computers from x64 to AArch64
  • Enhanced pseudo-random number generators
  • Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them.
  • The foreign function and memory API which allows Java programs to interoperate with code and data outside of the Java runtime
  • Removal of the Remote Method Invocation (RMI) Activation mechanism
  • Strong encapsulation for JDK internals
  • Deprecation of the Security Manager, preparing for removal in a future release
  • Context-specific deserialization filters
  • Deprecate the Applet API for Removal
  • Remove the Experimental AOT and JIT Compiler
  • Among many other new features and improvements

We will cover a few methods to get Java 17 installed on Rocky Linux 8 | CentOS 8 | CentOS 7.

  1. OpenJDK
  2. Oracle JDK/JRE.

1) Install OpenJDK 17 on Rocky Linux 8 | CentOS 8 | CentOS 7

Java OpenJDK 17 is an open source implementation of Java SE platform. OpenJDK is available on the RHEL 8 for x86_64 – AppStream repositories but the available version are not up-to-date. To download Open-source JDK 17 we use the Wget command as below:

We first install wget ro curl:

sudo yum -y install wget curl

Then proceed with your download:

### Linux 64-bit ###
wget https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz

### Linux ARM64 ###
wget https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-aarch64_bin.tar.gz

Then extract the package

### Linux 64-bit ###
tar xvf openjdk-17.0.2_linux-x64_bin.tar.gz

### Linux ARM64 ###
tar xvf openjdk-17.0.2_linux-aarch64_bin.tar.gz

Move the extracted file to the /opt/ directory as below.

sudo mv jdk-17.0.2/ /opt/jdk-17/

Set the Java Environment variables as below.

$ vim ~/.bashrc
export JAVA_HOME=/opt/jdk-17
export PATH=$PATH:$JAVA_HOME/bin 

$ source ~/.bashrc

Verify the Java installation.

$ echo $JAVA_HOME
/opt/jdk-17

$ java --version
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)

2) Install Oracle JDK 17 on Rocky Linux 8 | CentOS 8 | CentOS 7

Here we download the production ready OpenJDK from the Java SE Downloads page. Alternatively, one can use the Wget command as below but remember login is required upon visiting the official page. After login, obtain the download URL and use it as below.

wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm

Then install the ,rpm file.

sudo rpm -ivh jdk-17_linux-x64_bin.rpm

sample output.

warning: jdk-17_linux-x64_bin.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk-17-2000:17.0.2-ga            ################################# [100%]

By default, Java is installed in /usr/java/ directory. However you can install Java in a custom path using.

sudo rpm -ivh –prefix=// .rpmfile

Validate installation of Java 17 on Rocky Linux 8 / CentOS 8 / CentOS 7:

$ java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)

$ javac -version
javac 17.0.2

Set Java home:

$ sudo vi /etc/profile
export JAVA_HOME=/usr/java/default
$ source /etc/profile
$ echo $JAVA_HOME
/usr/java/default

3) Set Default Java Version on Rocky Linux 8 | CentOS 8 | CentOS 7

With multiple Java versions installed on your system, you can set the default one with the following steps:

First list the available versions:

sudo alternatives --config java

In the output, you should see the versions installed on your system.

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/java/jdk-17/bin/java
   2           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.12.0.7-0.el8_4.x86_64/bin/java)

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

Select your desired version by entering the number as above.

Now verify the set version using:

$ java -version
java version "17" 2021-09-14 LTS
Java(TM) SE Runtime Environment (build 17+35-LTS-2724)
Java HotSpot(TM) 64-Bit Server VM (build 17+35-LTS-2724, mixed mode, sharing)

4) Set the JAVA_HOME Environment Variable.

The JAVA_HOME environment variable is used by some Java applications to determine the install location of Java and the version to use in running the application.

Here we want to set a persistent path. We will edit the file in /etc/profile.d directory.

sudo vi /etc/profile

In the file, add this line.

JAVA_HOME="/path/to/java/install"

For the set changes to take effect, we either log out and login or use the source command as below:

source /etc/profile

Verify if the variable was set correctly by executing:

$ echo $JAVA_HOME
/usr/java/default

5) Test Java Installation

With Java installed successfully, let’s test it by creating a simple HTML file.

Create the file with the name Hello_World.java with the message as below:

cat > Hello_World.java <<EOF
public class helloworld {
  public static void main(String[] args) {
    System.out.println("Hello Java World from Kenya!");
  }
}
EOF

Compile the code.

java Hello_World.java

Sample Output:

$ java Hello_World.java
Hello Java World from Kenya!

Conclusion.

We have come to the end of our Guide. I have demonstrated the two ways to get Java 17 LTS Installed on Rocky Linux 8 | CentOS 8 | CentOS 7, configuring and setting the default Java version for your system.

No comments:

Post a Comment

So sánh các GitFlow model và áp dụng với CICD

https://medium.com/oho-software/so-s%C3%A1nh-c%C3%A1c-gitflow-model-v%C3%A0-%C3%A1p-d%E1%BB%A5ng-v%E1%BB%9Bi-cicd-b6581cfc893a