This tutorial shows you how to install OpenJDK 12 and Oracle JDK 12 on your Linux machine with the following approaches.

1. Archive Binaries (.tar.gz): This is the preferred option for most users. Both OpenJDK and Oracle JDK are avilable in the archive binary formats.

2. RPM Packages (.rpm): This is the mostly-used approach in RedHat and SuSE environments. Only the Oracle JDK is available as an RPM package.

Oracle JDK is no longer completely free for enterprise users. Make sure you grab a license before using it in production. However, it’s still free to download for development and testing purposes.

1. Installing from archive binaries (.tar.gz)

In this metod, the JDK is downloaded and extracted to a preferred location. Then install it manually via update-alternatives --install

1.1 Download the preferred JDK archive.

// OpenJDK
$ wget https://download.java.net/java/GA/jdk12/GPL/openjdk-12_linux-x64_bin.tar.gz

// Oracle JDK
$ wget https://download.oracle.com/otn-pub/java/jdk/12+33/312335d836a34c7c8bba9d963e26dc23/jdk-12_linux-x64_bin.tar.gz

1.2 Extract it to /usr/lib/jdk location.

$ sudo mkdir /usr/lib/jdk

// OpenJDK
$ sudo tar -xvzf openjdk-12_linux-x64_bin.tar.gz -C /usr/lib/jdk

// Oracle JDK
$ sudo tar -xvzf jdk-12_linux-x64_bin.tar.gz -C /usr/lib/jdk

1.3 Open the environment variables file with your favorite editor.

// vim
sudo vi /etc/environment

// nano
sudo nano /etc/environment

// gedit
sudo gedit /etc/environment

1.4 On the opened file, append the JDK’s bin directory path to the existing PATH variable.

/usr/lib/jdk/jdk-12/bin

1.5 At the end of the opened file, add the JAVA_HOME environment variable too.

JAVA_HOME="/usr/lib/jdk/jdk-12"

1.6 The final file must look like below.

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jdk/jdk-12/bin"
JAVA_HOME="/usr/lib/jdk/jdk-12"

1.7 Reload the environment variables for changes to get applied.

source /etc/environment

1.8 Run the following commands to setup the default Java locations.

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jdk/jdk-12/bin/java" 0

sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jdk/jdk-12/bin/javac" 0

sudo update-alternatives --set java /usr/lib/jdk/jdk-12/bin/java

sudo update-alternatives --set javac /usr/lib/jdk/jdk-12/bin/javac

1.9 List and verify all the installed Java versions with locations.

update-alternatives --list java

update-alternatives --list javac

1.10 Check the currently-using Java version.

java -version

javac -version

1.11 Multiple JDK versions can coexist in a system and we can change the default version as per our requirements at any time.

sudo update-alternatives --config java

2. Installing from RPM packages (.rpm)

2.1 Download the RPM-based version of JDK.

wget https://download.oracle.com/otn-pub/java/jdk/12+33/312335d836a34c7c8bba9d963e26dc23/jdk-12_linux-x64_bin.rpm

2.2 Get root user access to the system.

su

2.3 Install the downloaded RPM package.

rpm -ivh jdk-12_linux-x64_bin.rpm

2.4 This will create a new directory: /usr/java/jdk-12 for this installation of Java.

2.5 Multiple JDK versions can coexist in a system and we can change the default version as per our requirements at any time.

sudo update-alternatives --config java

👉 Any questions? Please comment below.


Leave a comment