Java has a runtime (the “JRE”, or “Java Runtime Environment”) which is required to run Java applications. There’s also a “JDK” (“Java Development Kit”) which is required to develop applications in Java. There are numerous blogs out there that explain how to install Java from repositories. This blog post will detail how to download and install Oracle’s Java SE manually.
Download Oracle Java SE
Oracle Java SE can be downloaded from:
Click on the Download button for the JDK version you wish to install. There are many Linux options available to download, but the rest of this blog post will assume you downloaded the
Extract the Oracle Java SE
Extract the tar file you downloaded to the directory you wish to install Java in. For example, if you downloaded Java 12 (
jdk-12_linux-x64_bin.tar.gz) and you wish to install Java to
[syd:z97] Downloads $ sudo tar -zxvf jdk-12_linux-x64_bin.tar.gz -C /usr/local/lib/
[sudo] password for syd:
[syd:z97] Downloads $ ls -l /usr/local/lib/
drwxr-xr-x 9 root root 4096 Apr 7 23:32 jdk-12
[syd:z97] Downloads $
Set $JAVA_HOME and update $PATH
Create an environment named
JAVA_HOME, and set it to point to the directory you installed Java to (in this example,
/usr/local/lib/jdk-12). Also add the bin directory (i.e.
/usr/local/lib/jdk-12/bin) to your system’s path. I normally make these changes system-wide by creating
/etc/profile.d/java.sh that contains:
Run (source) the above using:
$ source /etc/profile.d/java.sh
$ . /etc/profile.d/java.sh
You could stop there, and Java will work:
$ javac -version
But wait! There’s more!!!
You may find yourself in a situation where you need to work on multiple Java projects that use different versions of the JDK. It is possible to quickly switch between different versions of the JDK using update-alternatives.
Register your JDK install with
$ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/lib/jdk-12/bin/java" 1
$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/lib/jdk-12/bin/javac" 1
You can view how many “alternatives” for
javac you have using:
$ update-alternatives --list javac
To switch between different versions of javac:
$ sudo update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).
Selection Path Priority Status
0 /usr/local/lib/jdk-12/bin/javac 1 auto mode
1 /usr/local/lib/jdk-12/bin/javac 1 manual mode
2 /usr/local/lib/jdk1.8.0_201/bin/javac 1 manual mode
Press to keep the current choice[*], or type selection number:
Don’t forget if you switch your javac version, you should also switch your java version too:
$ sudo update-alternatives --config java
Assuming I want to uninstall JDK 12 which is installed in
$ sudo update-alternatives --remove "javac" "/usr/local/lib/jdk-12/bin/javac"
$ sudo update-alternatives --remove "java" "/usr/local/lib/jdk-12/bin/java"
$ sudo rm -rf /usr/local/lib/jdk-12
After this, clean up any files that set
$JAVA_HOME or add the JDK directory to
$PATH (in my case, this is done in