This guide will discuss the steps needed to install Java 12 on CentOS 7/6 & Fedora. Java 12 reached General Availability on 19 March 2019 with Production-ready binaries released. Install JDK/OpenJDK 12 on your CentOS/Fedora system to configure Java development environment.
For Ubuntu, use How To Install Oracle Java 12 on Ubuntu
Install OpenJDK 12 on CentOS 7/6 & Fedora
OpenJDK is a free and open-source implementation of the Java Platform, Standard Edition licensed under the GNU General Public License version 2.
Visit JDK 12 releases page to download the latest version.
curl -O https://download.java.net/java/GA/jdk12.0.1/69cfe15208a647278a19ef0990eea691/12/GPL/openjdk-12.0.1_linux-x64_bin.tar.gz
Extract downloaded file using tar command.
tar xvf openjdk-12.0.1_linux-x64_bin.tar.gz
Move the resulting folder to /opt directory.
sudo mv jdk-12.0.1 /opt/
Configure Java environment:
cat <<EOF | sudo tee /etc/profile.d/jdk12.sh
export JAVA_HOME=/opt/jdk-12.0.1
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
Add:
Source your profile file and check java
command
source /etc/profile.d/jdk12.sh
Confirm Java version.
$ echo $JAVA_HOME
/opt/jdk-12.0.1
$ java --version
openjdk 12.0.1 2019-04-16
OpenJDK Runtime Environment (build 12.0.1+12)
OpenJDK 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)
$ which java
/opt/jdk-12.0.1/bin/java
Install Java SE Development Kit 12 on CentOS & Fedora
If you choose to go with Java SE Development Kit 12, download RPM package for CentOS / RHEL system using the command below.
curl -LO -H "Cookie: oraclelicense=accept-securebackup-cookie" \
"https://download.oracle.com/otn-pub/java/jdk/12.0.1+12/69cfe15208a647278a19ef0990eea691/jdk-12.0.1_linux-x64_bin.rpm"
Then install the package with the rpm
command:
$ sudo rpm -Uvh jdk-12.0.1_linux-x64_bin.rpm
warning: jdk-12.0.1_linux-x64_bin.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing… ################################# [100%]
Updating / installing…
1:jdk-12.0.1-2000:12.0.1-ga ################################# [100%]
Confirm Java version installed
$ java -version
java version "12.0.1" 2019-04-16
Java(TM) SE Runtime Environment (build 12.0.1+12)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)
Configure Java environment.
cat <<EOF | sudo tee /etc/profile.d/jdk12.sh
export JAVA_HOME=/usr/java/default
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
To use Java Home, source the file.
source /etc/profile.d/jdk12.sh
Test Java Installation
Create a HelloWorld Java program.
$ cat HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
Compile Java code.
$ javac HelloWorld.java
Run your Java program.
$ java HelloWorld
Hello, World
Choosing Default Version of Java
If you have more than one version of Java installed, you can set default one using alternatives command.
sudo alternatives --config java
Recommended books:
For Java 11 installation, see our guides below.
How to Install Java 11 on CentOS 7 / Fedora
How to Install Java 11 (OpenJDK 11) on RHEL / CentOS 8
How to Install Java 11 on Ubuntu / Debian