Java Installation in Ubuntu Effortless Method

Published by Diwakar on

How to install java in Ubuntu
Spread the love

Java can be installed in Form of JRE or JDK as per need. for more details on JDK, JRE, JVM check here.

Below guide will take you to complete the Java Installation in Ubuntu with the installation of JDK :-
i. Open JDK
ii. Oracle JDK

First Verify if any Java is already installed on Server/Sytem by executing below command

java -version

If no version is found by above command then we can proceed installation with below guide.

Installation of Open JDK

Open JDK Installation is very simple in Linux Environment and One Single Line Command does it all.

sudo apt install default-jdk -y

That’s it, we have successfully installed Open JDK.

Installation of Oracle JDK

Oracle JDK installation requies accepting license of ORACLE on their site.
This can be directly downloaded from here , we just need to choose appropriate version based on operating system on which we need to install JDK and accept the terms and condition.

Commercially Oracle provides a Licensed Version of Java Development Kit but for Development need or Personal use it is available free of cost.

If Our System doesn’t have any open JDK Installation then we can skip step 1, step 9 and step 10 from below tutorial.

Step 1 : Removing Open JDK

If our system/server has any Open JDK installation available and we do not need OpenJDK then first we need to remove existing installation of Open JDK to avoid further conflicts.

apt-get purge openjdk-*

Step 2 : Download and Extract the Compressed Distribution of Java

Create a folder name java in /usr/local by below command
sudo mkdir -p /usr/local/java

Copy the downloaded file mentioned above to the directory which we created

sudo cp -r jdk-8u111-linux-x64.tar.gz /usr/local/java/

move into /usr/local/java directory and extract the copied file by

cd /usr/local/java/
sudo tar xvzf jdk-8u111-linux-x64.tar.gz

After extraction you must see a folder jdk-8u***-linux-x64 .

Update the path file by opening profile file
sudo nano /etc/profile

and edit java version and paste the following at the end of the file

JAVA_HOME=/usr/local/java/jdk1.8.{your_version}
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH

Save and Exit

If open jdk or any other java version is installed earlier that we have removed in the very first step then we need to tell the system that the new oracle java version is availably by (change java version in command by the version you have downloaded)

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.8.0_111/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.8.0_111/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk1.8.0_111/bin/javaws" 1

Make Orcale Java JDK as the default by

update-alternatives --set java /usr/local/java/jdk1.8.0_111/bin/java
update-alternatives --set javac /usr/local/java/jdk1.8.0_111/bin/javac
update-alternatives --set javaws /usr/local/jdk1.8.0_111/bin/javaws

Reload the system wide path by

source /etc/profile

Reboot the system as sometimes java alternatives are updated only after server reboots.
reboot

Check the Java JDK Version by
java -version

It’s completed now, we have succesfully installed Oracle JDK in our server.

for setup and installation of Tomcat in Ubuntu or Linux environment please refer this link.