How to Install Java/JDK on Ubuntu 18.04

Last updated on Mar 15,2023 20.4K Views
Research Analyst, Tech Enthusiast, Currently working on Azure IoT & Data Science... Research Analyst, Tech Enthusiast, Currently working on Azure IoT & Data Science with previous experience in Data Analytics & Business Intelligence.

How to Install Java/JDK on Ubuntu 18.04

edureka.co

Oracle Java JDK (Java Development Kit) is a development environment for developing applications and tools based on Java. It is a versatile kit which can be used for testing applications, and program development alike, using the Java programming language. This is going to be a brief guide for beginners on how to download and install java on Ubuntu 18.04. This is essential for any professional looking into an opportunity in Linux Administration.

You could also install Open Java JDK / JRE (an open-source alternative)  using the apt-get command, pretty easily. There are plenty of tutorials out there which may show you how to get Java installed via third-party PPA tools. However, this article is focused on two fool-proof methods to download Java from its original repository instead of the opensource version of via third-party.

So, if you follow these few simple steps, you should be able to download and install JDK on your Operating Systems, completely hassle-free.

Oracle Website

Step 1: Visit the Oracle Website

 

Step 2: Java SE Development Kit

Download & Install Java on Ubuntu using the tarball (Method 1)

Step 1: Download tarfile

Step 2: Extract files

tar -zxvf ~/Downloads/jdk-12.0.1_linux-x64_bin.tar.gz

sudo mkdir -p /usr/lib/jvm/jdk-12.0.1/

sudo mv jdk-12.0.1_linux-x64 /usr/lib/jvm/jdk12.0.1/

Download & Install Java on Ubuntu using the deb package (Method 2)

Step 1: Download deb package

cd /tmp

wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/12.0.1+33/312335d836a34c7c8bba9d963e26dc23/jdk-12.0.1_linux-x64_bin.deb

Step 2: Install Oracle Java

sudo dpkg -i jdk-12.0.1_linux-x64_bin.deb

Step 3: Configure Java on your system

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-12.0.1/bin/java 2

sudo update-alternatives --config java

Suppose you have other versions of Java installed and you ran the commands above, you should see a prompt to select the version of Java you want to make the default. If you don’t have other versions of Java installed, then the commands will not return anything.

sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk-12.0.1/bin/jar 2

sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-12.0.1/bin/javac 2

sudo update-alternatives --set jar /usr/lib/jvm/jdk-12.0.1/bin/jar

sudo update-alternatives --set javac /usr/lib/jvm/jdk-12.0.1/bin/javac

That should get Java installed and configured.

java -version

You should expect to see the output below:

java 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)

Step 4: Create Java Environment Variables

sudo nano /etc/profile.d/jdk12.0.1.sh

export J2SDKDIR=/usr/lib/jvm/java-12.0.1

export J2REDIR=/usr/lib/jvm/java-12.0.1

export PATH=$PATH:/usr/lib/jvm/java-12.0.1/bin:/usr/lib/jvm/java-12.0.1/db/bin

export JAVA_HOME=/usr/lib/jvm/java-12.0.1

export DERBY_HOME=/usr/lib/jvm/java-12.0.1/db

source /etc/profile.d/jdk12.0.1.sh

java -version

java 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)


Congratulations! You’ve just successfully installed Java/JDK 12.0.1 on Ubuntu 18.04.

 Edureka’s Linux Administration Certification Training is curated to shape your career as a Linux professional & help you to run applications, perform desired functions on your system and networks, create a network configuration, and maintain security administration.

Upcoming Batches For Linux Administration Certification Training Course
Course NameDateDetails
Linux Administration Certification Training Course

Class Starts on 22nd February,2025

22nd February

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES