Java is an object-oriented programming language developed by Sun Microsystems since 1991 and officially released on May 23, 1995. The new programming language was originally called Oak (James Gosling) and was developed for consumer electronics, but later it was renamed Java and began to be used for writing applets, applications and server software
A peculiarity of Java technology is a flexible security system due to the fact that the program execution is fully controlled by a virtual machine. Any operation that exceeds the program's set authority (e.g. attempting unauthorized access to data or connecting to another computer) causes an immediate interruption.

file

Installing the default JRE/JDK

The easiest way to install Java is to use the version distributed with Ubuntu.

To install, update the package index:
``.shell
apt-get update && apt-get upgrade


Then let's install Java. This command will install the Java Runtime Environment (JRE):  
~~~.shell
sudo apt-get install default-jre  
~~~
There is a second version of the default installation of Java: the JDK (Java Development Kit). Installing the JDK is usually necessary when you plan to compile Java programs or when the software you are using explicitly requires the JDK.  

You can install the JDK with the following command:  
~~~.shell
sudo apt-get install default-jdk  
~~~

#### Installing Oracle JDK
To install Oracle JDK, which is the official version of Java distributed by Oracle, you need to follow a few extra steps.  

First, let's add the Oracle PPA:  
~~~.shell
sudo add-apt-repository ppa:linuxuprising/java  
~~~
~~~.shell
root@kvmde54-19861:~# sudo add-apt-repository ppa:linuxuprising/java  
 Oracle Java 11 (LTS) and 14 installer for Ubuntu (20.04, 19.10, 18.04, 16.04 and 14.04), Linux Mint and Debian.

Java binaries are not hosted in this PPA due to licensing reasons. The packages in this PPA download and install Oracle Java, so a working Internet connection is required.  

The packages in this PPA are based on the WebUpd8 Oracle Java PPA packages: https://launchpad.net/~webupd8team/+archive/ubuntu/java  

Created for users of https://www.linuxuprising.com/  

Installation instructions (with some tips), feedback, suggestions, bug reports etc.:  

Oracle Java 11: https://www.linuxuprising.com/2019/06/new-oracle-java-11-installer-for-ubuntu.html  
Oracle Java 14: https://www.linuxuprising.com/2020/03/how-to-install-oracle-java-14-jdk14-on.html  

Important notice regarding Oracle Java 11 and newer: the Oracle JDK license has changed starting April 16, 2019. The new license permits certain uses, such as personal use and development use, at no cost -- but other uses authorized under prior Oracle JDK licenses may no longer be available. A FAQ is available here: https://www.oracle.com/technetwork/java/javase/overview/oracle-jdk-faqs.html . After this change, new Oracle Java 11 releases (11.0.3 and newer) require signing in using an Oracle account to download the binaries. This PPA has a new installer which requires the user to download the Oracle JDK 11 .tar.gz and place it in a folder, and only then install the "oracle-java11-installer-local" package. Details here: https://www.linuxuprising.com/2019/06/new-oracle-java-11-installer-for-ubuntu.html  

About Oracle Java 10, 12 and 13: These versions have reached the end of public updates, therefore they are longer available for download. The Oracle Java 10/12/13 packages in this PPA no longer worked due to this, so I have removed them. Switch to Oracle Java 11 or OpenJDK 11 instead, which is long term support, or the latest Java 14.  
 More info: https://launchpad.net/~linuxuprising/+archive/ubuntu/java
Press [ENTER] to continue or ctrl-c to cancel adding it  

gpg: keyring `/tmp/tmpyvsm7f7w/secring.gpg' created  
gpg: keyring `/tmp/tmpyvsm7f7w/pubring.gpg' created  
gpg: requesting key 73C3DB2A from hkp server keyserver.ubuntu.com  
gpg: /tmp/tmpyvsm7f7w/trustdb.gpg: trustdb created  
gpg: key 73C3DB2A: public key "Launchpad PPA for Linux Uprising" imported  
gpg: total number processed: 1  
gpg: imported: 1 (RSA: 1)  
OK

~~~
Then update the list of packages:  
~~~.shell
@@
sudo apt-get update  
~~~
Next, depending on which specific version you want to install, run one of the following commands:  

For example, let's install Oracle JDK 14  
To install, run the command:  
~~~.shell
sudo apt-get install oracle-java14-installer  
~~~
~~~.shell
root@kvmde54-19861:~# java -version  
java version "14.0.1" 2020-04-14  
Java(TM) SE Runtime Environment (build 14.0.1+7)  
Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)

~~~

#### Java Version Management

More than one version of Java can be installed on the same server at the same time. You can use the update-alternatives utility to set the default version and create symbolic links to different versions.  
~~~.shell
sudo update-alternatives --config java  
~~~
The output of this command will be similar to the one below. In this example, we see that all Java versions mentioned above have been installed.  
~~~.shell
root@kvmde54-19861:~# update-alternatives --config java  
There are 2 choices for the alternative java (providing /usr/bin/java).  

  Selection Path Priority Status
------------------------------------------------------------
  0 /usr/lib/jvm/java-14-oracle/bin/java 1091 auto mode
* 1 /usr/lib/jvm/java-14-oracle/bin/java 1091 manual mode
  2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode

Press <enter>to keep the current choice[*], or type selection number  
~~~

You can select the number of the Java version you want to use by default. You can do the same for the Java compiler (javac), the documentation generator (javadoc), the JAR signing tool (jarsigner) and other tools  
Enter the following command, replacing command with the name of the desired tool:  
~~~.shell
sudo update-alternatives --config command  
~~~

#### Setting the JAVA_HOME environment variable

To set the environment variable, you need to pre-define the Java installation directory. Use the command **update-alternatives**:  
``.shell
sudo update-alternatives --config java  

This command shows each Java installation and installation path:
``.shell
root@kvmde54-19861:~# sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status

0 /usr/lib/jvm/java-14-oracle/bin/java 1091 auto mode
* 1 /usr/lib/jvm/java-14-oracle/bin/java 1091 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode

Press to keep the current choice[*], or type selection number

Copy the path of the preferred version's installation directory. Open **/etc/environment** directory with a text editor:  
``.shell
sudo nano /etc/environment  

Add the following line at the end of this file and replace the path copied earlier:

JAVA_HOME="/usr/lib/jvm/java-14-oracle/bin/"  

Changing the file will set the path JAVA_HOME for all users on the system.

Save the changes and close the file (CTRL + X, Y, Enter)

Load the file again, to apply the changes to the current session:
``.shell
source /etc/environment

Check that the environment variable is set:  
``.shell
echo $JAVA_HOME  

You will see the path you just set:

root@kvmde54-19861:~# echo $JAVA_HOME  
/usr/lib/jvm/java-14-oracle/bin/

Other users will need to run the command source /etc/environment to apply this parameter.


If you have configuration difficulties or have further questions, you can always contact our support team via ticket system.

Updated Sept. 15, 2024