How to install Java on Ubuntu

If you ask what Java is, you might be told of Java as a general-purpose, object-oriented language that looks a lot like C and C ++, but is easier to use and allows you to create more robust programs. Unfortunately, this definition will not give you a complete understanding of Java. A more detailed definition is provided by Sun Microsystems and is just as relevant, although it was announced way back in 2000:

Java is a simple, object-oriented, network-enabled, interpretable, reliable, secure, architecture-neutral, portable, high-performance, multithreaded, dynamic programming language.

Installing the JRE/JDK

One of the easiest ways to install Java is to install the version distributed with Ubuntu. It comes with the package!

To install,  let’s start with the usual update and upgrade commands.

apt-get update && apt-get upgrade  


Then, proceed to install Java. This command will install the JRE - Java Runtime Environment:

sudo apt-get install default-jre  


If you’re planning to develop stuff on Java then you need to install the JDK (Java Development Kit). Installing the JDK is usually necessary when you plan to compile Java programs or when the software you use explicitly requires the JDK.

You can install the JDK with the following command:

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, add the Oracle PPA:

 

sudo add-apt-repository ppa:linuxuprising/java  


root@ubuntu-KVM:~# sudo add-apt-repository ppa:linuxuprising/java  


Then you need to update the list of packages once again:

sudo apt-get update  


Next, you need to decide what kind of version you want to be installed. 

For example, install Oracle JDK 17 - the latest one according to Oracle website as of 2022.

To install, run the command:

sudo apt-get install oracle-java17-installer  


root@ubuntu-KVM:~# java -version 

java version "17.0.2" 2022-02-09 

Java(TM) SE Runtime Environment (build 17.0.2) 

Java HotSpot(TM) 64-Bit Server VM (build 17.0.2, mixed mode, sharing)

How to update different Java versions on Ubuntu

More than one version of Java can be installed on the same server at the same time. You can use the update-alternatives command to set the default version and create symbolic links to different versions.

sudo update-alternatives --config java  


You’ll receive an output similar to the one below. In this example we see that all Java versions mentioned above have been installed.

root@ubuntu-KVM:~# 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-17-oracle/bin/java             1091      auto mode

* 1            /usr/lib/jvm/java-17-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 Java version number that will be used 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, e.g. ‘javac’:

sudo update-alternatives --config command


OR


sudo update-alternatives --config java 

Configuring the JAVA_HOME on Ubuntu

To set the environment variable, you must first define the Java installation directory. Use the update-alternatives command:

sudo update-alternatives --config java  


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 <enter> to keep the current choice[*], or type selection number:  


Copy the path of the preferred version's installation directory. Open /etc/environment in a text editor:

sudo nano /etc/environment  


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

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


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

Save the changes by hitting CTRL + X and close it with Enter.

Load the file again to apply the changes to the current session:

source /etc/environment  


Review that the environment variable is set correctly:

echo $JAVA_HOME  


You should see something like this:

/usr/lib/jvm/java-17-oracle/bin/


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

Conclusion

That’s it! Now you know how to install Java on Ubuntu server. It is not so difficult per se but has some gimmicks in the process. Make sure to contact BlueVPS if you have any questions regarding the installation. We are here to help!



Blog