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:
https://www.oracle.com/technetwork/java/javase/downloads/index.html
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 .tar.gz
version.
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 /usr/local/lib
:
[syd:z97] Downloads $ sudo tar -zxvf jdk-12_linux-x64_bin.tar.gz -C /usr/local/lib/
[sudo] password for syd:
jdk-12/bin/jaotc
jdk-12/bin/jar
jdk-12/bin/jarsigner
jdk-12/bin/java
jdk-12/bin/javac
...
[syd:z97] Downloads $ ls -l /usr/local/lib/
total 12
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:
export JAVA_HOME=/usr/local/lib/jdk-12
export PATH=$PATH:$JAVA_HOME/bin
Run (source) the above using:
$ source /etc/profile.d/java.sh
or just:
$ . /etc/profile.d/java.sh
You could stop there, and Java will work:
$ javac -version
javac 12
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 update-alternatives
:
$ 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
/usr/local/lib/jdk-12/bin/javac
/usr/local/lib/jdk1.8.0_201/bin/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
...
Uninstalling Java
Assuming I want to uninstall JDK 12 which is installed in /usr/local/lib/jdk-12
:
$ 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 /etc/profile.d/java.sh
).