To be able to run any Java application, we need first to install the Java Software Development Kit or abbreviated with JDK or Java SDK.

Firstly we need to understand what is JDK and what is SDK.

1. What is JDK

Java Development Kit (JDK) is a software development environment used for developing applications and applets. It is one of three core technology packages used in Java programming, along with Java Virtual Machine (JVM) and Java Runtime Environment (JRE). Developers that are just starting to learn Java often mix the JDK and JRE, so we will explain what is the difference between them.

JRE is the on-disk part of Java that creates the JVM, it can be used as a standalone component to simply run Java programs, but it is a part of JDK.

JDK allows us (developers) to create Java programs that can be executed and run by the JVM and JRE. The JDK needs JRE because running Java programs is also a part of developing them.

The main difference between JRE (Java Runtime Environment) and JDK (Java Development Kit) is that JRE is needed only for running Java programs, while JDK, includes JRE and other development tools (for example debugger and compiler), is used both for running and writing Java programs.

2. What is SDK or better to say what is Java SDK

A Software Development Kit (SDK) is typically a set of software development tools that allows the creation of applications for a certain software package, software framework and so on. A SDK can take a form of a simple implementation of one or more Application Programming Interfaces (API) in the form of on-device libraries to interface of a particular programming language.

To break this easier for all of us, SDK is made up of lines of code, written by a third party, that can be added to a digital applications to support new capabilities. Although SDK is mostly associated with mobile applications, it can also be used for websites or other digital platforms.

Now that we explained what are JDK and SDK, we will add a graph that shows how they are connected:

jdk-jre-jvm
JDK JRE and JVM

3. Installation of Java on Ubuntu - the easy way

We will update our repository for Ubuntu 16.04 and 18.04

sudo add-apt-repository -y ppa:linuxuprising/java
sudo apt-get update

3.1. Install JDK 12

sudo apt-get install -y oracle-java12-installer

3.2. Install JDK 11

sudo apt-get install -y oracle-java11-installer

3.3. Install JDK 10

sudo apt-get install -y oracle-java10-installer

3.4. Install JDK 9

sudo apt-get install -y oracle-java9-installer

3.5. Install JDK 8

sudo apt-get install -y oracle-java8-installer

3.6. Install JDK 7

sudo apt-get install -y oracle-java7-installer

4. Additional commands

show which java versions are installed with

sudo update-java-alternatives -l

update the environment (system variables JAVA_HOME and PATH)

sudo update-java-alternatives -s java-10-oracle

now test your setup

echo $JAVA_HOME

You should get result like this:

/usr/lib/jvm/java-10-oracle

If you do not get the proper paths, either re-login or better restart your machine since the update-java-alternatives is handling it system wide, which gets latest loaded on reboot.

Here you should get the Path to JDK.

If you want to check the version of Java, type the following:

java -version

You should get a result like this:

java version "10.0.2" 2018-07-17
JAVA(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)

4.1. You can also setup your paths manually, but !!! NOT !!! recommended

This is only recommended to experienced system administrators or experienced java developers. So skip it if not really needed

Now we will set a path for "JAVA_HOME", make sure you use your installation path

Now save and close the gedit file, and go back to the terminal and type the following:

gedit .bashrc

Now there will be a gedit window opened, scroll at the end and type the following:

JAVA_HOME=/usr/lib/jvm/java-10-openjdk
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

for a better understanding about JAVA_HOME you can read also How to set JAVA_HOME properly?

5. Installation on Ubuntu - Hard way

This way to install JDK on Ubuntu is manual, and longer, here are the steps to install it manually

If you want to install JDK 10, you should go to Oracle download site, and follow the next steps:

  1. Under "Java Platform, Standard Edition", choose JDK download button
  2. Choose the for which operating system you want to download (Windows, Linux, macOS)
  3. We will download it under folder Downloads

We will install it under "/usr/local/java", but to do it we have to create a "java" directory

cd /usr/local
sudo mkdir java

Now we have to extract the package, we have added our download path (make sure you write your filename correctly)

Note: in code we typed this {x}, that is a version of jdk you want to install (this is for jdk 10, but can also be applied for other version)

cd usr/local/java
sudo tar xzvf ~/Downloads/jdk-{x}_linux-x64_bin.tar.gz

Now we have to tell Ubuntu to use this JDK, but firstly we have to setup the locations of java, javac and

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk-10/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk-10/bin/javac" 1

Now that locations are set, we have to use them as default

sudo update-alternatives --set java /usr/local/java/jdk-10/bin/java 
sudo update-alternatives --set javac /usr/local/java/jdk-10/bin/javac

Now that is left is to verify the installation, here is how:

javac -version
java -version
which javac
which java

6. Installation on Windows

To install JDK on Windows, we firstly need to download it from the page and download Windows version (should be called: jdk-10.0.2_windows-x64_bin.exe)

To install the JDK just start the installer and follow the instructions.

You can set the PATH variable on Windows by doing this:

Open Control Panel and select System

development/java/jdkset PATH for jdk control panel system
open control panel system settings to set PATH

Choose the Advanced system settings and then Environment Variables

set PATH for jdk system properties
Open System properties

While in Environment Variables, scroll down (in System variables part) until you see a Path

set PATH for jdk edit environment variables
Open Environment Variables to set PATH

Double click on variable "Path" and you will be given an option to add your path to JDK (in variable value). We have added ours to C:\Program Files\Java\jdk-10\bin

set PATH for jdk edit system variable
Open Edit to set a PATH to JDK

If we had more paths to JDK, they would be divided by semicolon (;)

for a better understanding about JAVA_HOME you can read also How to set JAVA_HOME properly?