Advance

How to install Java with Apt on Ubuntu 20.04

How to install Java with Apt on Ubuntu 20.04
1
(1)

Recently, you read about Java and in this tutorial you will learn How to install Java with Apt on Ubuntu 20.04. As you experienced, java and the JVM (Java’s virtual machine) are required for many kinds of software, including  TomcatJettyGlassfish, Cassandra and Jenkins.

Join us to learn how to install various versions of the Java Runtime Environment (JRE) and the Java Developer Kit (JDK) using apt, and OpenJDK as well as the official JDK from Oracle. and then by selecting the version you wish to use for your projects. When you’re finished, you’ll be able to use the JDK to develop software or use the Java Runtime to run the software.

 

Prerequisites

The tutorial may be more useful if you consider:
a non-root user with sudo privileges
To set up, follow our Initial server setup on Ubuntu 20.04.

Recommended Article: How to Install and Configure oVirt on Ubuntu 20.04

How to install Java with Apt on Ubuntu 20.04

Let’s walk through the steps of this guide to see How to install Java with Apt on Ubuntu 20.04.

 

Installing the Default JRE/JDK

You will feel free to use the version packaged with Ubuntu to install Java. But since Ubuntu 20.04 includes Open JDK 11, which is an open-source variant of the JRE and JDK by default, you need to update the package index first.

sudo apt update

And to if Java is already installed:

java -version

But you’ll see the following output if Java is not currently installed.

Output
Command 'java' not found, but can be installed with:    sudo apt install default-jre              # version 2:1.11-72, or  sudo apt install openjdk-11-jre-headless  # version 11.0.7+10-3ubuntu1  sudo apt install openjdk-13-jre-headless  # version 13.0.3+3-1ubuntu2  sudo apt install openjdk-14-jre-headless  # version 14.0.1+7-1ubuntu1  sudo apt install openjdk-8-jre-headless   # version 8u252-b09-1ubuntu1

Now, you can use the following command to install the default Java Runtime Environment (JRE), which will install the JRE from OpenJDK 11:

sudo apt install default-jre

The JRE will allow you to run almost all Java software.

To verify the installation:

java -version
Output
openjdk version "11.0.7" 2020-04-14  OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)  OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

You may need the Java Development Kit (JDK) in addition to the JRE in order to compile and run some specific Java-based software. So, use the command below to install the JDK, which will also install the JRE:

sudo apt install default-jdk

Verify that the JDK is installed by checking the version of javac, the Java compiler:

javac -version
Output
javac 11.0.7

buy VPS with bitcoin

 

Installing Oracle JDK 11

Since the Oracle’s licensing agreement for Java doesn’t allow automatic installation through package managers. You must create an Oracle account and manually download the JDK to add a new package repository for the version you’d like to use to install the Oracle JDK, which is the official version distributed by Oracle.

Note: The version of Oracle’s JDK you’ll need to download must match the version of the installer script. To find out which version you need, visit the oracle-java11-installer page.

Locate the package for Focal, as shown in the following figure:

 

finding the package for Focal
Since the version of the script is 11.0.7, you need Oracle JDK 11.0.7. But you don’t need to download anything from this page, only download the installation script through apt shortly.

After that, you must visit the Downloads page and locate the version that matches the one you need.

 

Download page

 

By clicking the JDK Download button you will be taken to a screen that shows the versions available. Click the .tar.gz package for Linux.

 

Java SE development

Then, you will face a screen asking you to accept the Oracle license agreement. Select the checkbox to accept the license agreement and press the Download button. Your download will begin. You may need to log in to your Oracle account one more time before the download starts.

You’ll need to transfer it to your server when the file has downloaded. On your local machine, upload the file to your server. On macOS, Linux, or Windows using the Windows Subsystem for Linux, use the scp command to transfer the file to the home directory of your noodi user. The following command assumes you’ve saved the Oracle JDK file to your local machine’s Downloads folder:

scp Downloads/jdk-11.0.7_linux-x64_bin.tar.gz noodi@your_server_ip:~

Next, return to your server and add the third-party repository that will help you install Oracle’s Java when the file upload has completed.

Install the software-properties-common package, which adds the add-apt-repository command to your system:

sudo apt install software-properties-common

Then, import the signing key used to verify the software you’re about to install:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2
Output
gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" imported  gpg: Total number processed: 1  gpg:               imported: 1

Next, to add the repo to your list of package sources, use the add-apt-repository command.

sudo add-apt-repository ppa:linuxuprising/java

Output

 Oracle Java 11 (LTS) and 12 installer for Ubuntu, Linux Mint and Debian.    Java binaries are not hosted in this PPA due to licensing. The packages in this PPA download and install Oracle Java 11, 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.:    . . .    Press [ENTER] to continue or ctrl-c to cancel adding it

 

To continue the installation, press ENTER to see a message about no valid OpenPGP data found, but you can safely ignore this.

Update your package list to make the new software available for installation:

sudo apt update

The installer will look for the Oracle JDK you downloaded in /var/cache/oracle-jdk11-installer-local. Create this directory and move the Oracle JDK archive there:

sudo mkdir -p /var/cache/oracle-jdk11-installer-local/  sudo cp jdk-11.0.7_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/  

Finally, install the package:

sudo apt install oracle-java11-installer-local

 

Managing Java

In case you multiple Java installations on one server, configure which version is the default for use on the command line by using the update-alternatives command.

sudo update-alternatives --config java
Output
There are 2 choices for the alternative java (providing /usr/bin/java).      Selection    Path                                         Priority   Status  ------------------------------------------------------------    0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      auto mode    1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode  * 2            /usr/lib/jvm/java-11-oracle/bin/java          1091      manual mode    Press <enter> to keep the current choice[*], or type selection number:

Then, press ENTER to leave the current settings in place or choose the number associated with the Java version to use it as the default.

Also, you can do this for other Java commands, such as the compiler (javac):

sudo update-alternatives --config javac

Other commands for which this command can be run include, but are not limited to keytool, javadoc, and jarsigner.

Setting the JAVA_HOME Environment Variable

Please be aware that many programs are written using Java use the JAVA_HOME environment variable to determine the Java installation location.

To set this environment variable, first determine where Java is installed.

sudo update-alternatives --config java
Output
There are 2 choices for the alternative java (providing /usr/bin/java).      Selection    Path                                         Priority   Status  ------------------------------------------------------------    0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      auto mode    1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode  * 2            /usr/lib/jvm/java-11-oracle/bin/java          1091      manual mode    Press <enter> to keep the current choice[*], or type selection number:

In this case, the installation paths are as follows:

  1. OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
  2. Oracle Java is located at /usr/lib/jvm/java-11-oracle/jre/bin/java.

Copy the path from your preferred installation. Then open /etc/environment using nano or your favorite text editor:

sudo nano /etc/environment

At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path, but do not include the bin/ portion of the path:

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

Modifying this file will set the JAVA_HOME path for all users on your system.

Now, you can save the file and exit the editor.

Next, reload this file to apply the changes to your current session:

source /etc/environment

To verify that the environment variable is set or not:

echo $JAVA_HOME
Output
/usr/lib/jvm/java-11-openjdk-amd64

You need to use the source /etc/environment or log out and log back in to apply this setting as other users.

Recommended Article: How to install Java with Apt on Ubuntu 20.04

Good job! You’re all set! you have installed multiple versions of Java and learned how to manage them. You can now install software that runs on Java, such as Tomcat, Jetty, Glassfish, Cassandra, or Jenkins.

How useful was this post?

Click on a star to rate it!

Average rating 1 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

View More Posts
Tom Veitch
Eldernode Writer
We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave a Reply

Your email address will not be published. Required fields are marked *

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+8595670151

7 days a week, 24 hours a day