Search at Knowledgebase
How to install Java on Ubuntu 18.04

Objective

Java is a software platform that allows the compilation and execution of software written in Java language. Java features a cross-platform to compile and execute applications no matter what the hardware in use is.
First, connect to your server via an SSH connection. If you haven’t done so yet, following our guide is recommended to connect securely with SSH. In case of a local server, go to the next step and open the terminal of your server.

Installing Java – JRE

Verify that Java is not already installed with the following command:

$ java - version

If the following message appears on the screen:

Command 'java' not found

Java is not installed on the system. So, proceed with the installation of the Java Runtime Environment which runs most Java softwares.
Update the distribution repositories, to be sure you download the latest version of the packages:

$ sudo apt update

Then, start the installation of the Java Runtime Environment:

$ sudo apt install default-jre

At this point, check the correct installation:

$ java - version

If the installation was successful, the command will show the version number of the JRE.

Installing OpenJDK

To use the Java Development Kit to compile or run Java software, proceed with its installation in this way:

$ sudo apt install default-jdk

Check the installation by showing the version number of javac, the Java compiler:

$ javac - version

If the installation was successful, the version number of javac will be shown:

javac 10.0.0

Managing multiple versions of Java

You can set the default Java version using the update-alternatives command:

$ sudo update-alternatives --config java

This selection screen will be shown:

There are 3 choices for the alternative java (providing /usr/bin/java).
                  Selection    Path                                   Priority Status

------------------------------------------------------------

* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101 auto mode

  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101 manual mode

  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081 manual mode

  3            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081 manual mode

Type the number associated with the Java version of your preference you to set it as ‘default’ or press ENTER to keep the current version as default.

Setting the JAVA_HOME environment variable

Different software use the JAVA_HOME variable to locate the version of java to be used.
To set the version to use, first, check the current Java installations on the system using the update-alternatives command:

$ sudo update-alternatives --config java

The following screen, showing the path of each Java installation, will be displayed:

There are 3 choices for the alternative java (providing /usr/bin/java).
                  Selection    Path                                   Priority Status

------------------------------------------------------------

* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101 auto mode

  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101 manual mode

  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081 manual mode

  3            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081 manual mode

For example, o use version 11, copy the following path:

/usr/lib/jvm/java-11-openjdk-amd64/bin/

Now edit the /etc/ environment file:

$ sudo nano /etc/environment

and, at the end of the file, add the following line:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/bin/"

Save and close the file.
At this point, the path relative to the JAVA_HOME variable for all the users of the system, will have been modified
Reload the file with the source command to apply the changes:

$ source /etc/environment

and check whether the variable has been correctly set by printing it on the screen.

$ echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-amd64/bin/
Your FREE Trial Starts Here!
Contact our team for application of dedicated server service!
Register as a member to enjoy exclusive benefits now!
Your FREE Trial Starts here!
Contact our team for application of dedicated server service!
Register as a member to enjoy exclusive benefits now!
Telegram Skype