How to install Java in Ubuntu?
JAVA is an open-source programming language. It is an object-arranged programming language intended for having lesser execution conditions. It is a computing source for application development. Released in 1995, and since then, it has come with various latest versions with continuous improvements. Java’s most recent version incorporates many updates that improve the speed, reliability, and security system of Programs in Java.
Thus, Ubuntu also incorporates Java for creating apps and software programs for other services. Java development kit (open JDK) and Java Runtime (open JRE) are the two main java packages available in the Ubuntu repository. According to your needs, you can add any of the packages on Ubuntu. Thus if you are looking to install Java on Ubuntu, then this guide is intended for you.
There are four different versions of JAVA which are as follows:-
- Java SE (Java Standard Edition)
- Java ME (Java Micro Editions)
- Java Enterprise Edition (Java EE)
- JavaFX
To begin with, it would be great to use the version packaged with Ubuntu to install Java. Ubuntu comes as 18.4 by default with JDK 11.
You Need to Update the Package Index, for Installation of this Version;
sudo apt update
Then you have to check whether Java is already installed or not;
java -version
You will get an output below, if the Java is not installed on the system yet;
Output
Command ‘java’ not found, but can be installed with:
sudo apt install default-jre
sudo apt install openjdk-11-jre-headless
sudo apt install openjdk-8-jre-headless
To install the default java Runtime Environment (JRE), use the following command as below. It will install the JRE from open JDK 11;
sudo apt install default-jre
To verify the installation, enter the command;
java -version
You will get an output;
Output
openjdk version “11.0.11” 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.18.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.18.04, mixed mode, sharing))
To run some particular Java based software, you could also require a Java development kit (JDK).
For the JDK installation, you have to use the below given command;
sudo apt install default-jdk
Now, you can check the version of Java to ensure the installation of JDK;
javac -version
Output
javac 11.0.11
Further, you have to install Oracle’s JDK and JRE
Installing Oracle JDK 11
Automatic installation with package managers is not available in Oracle’s licensing terms for Java. Thus, an Oracle account is mandatory to download JDK and to add a new package repository for installing the Oracle JDK.
Further, use the apt to begin the installation with a third party script.
It is important to download a similar version of JDK as with the installer script.
Once the file gets downloaded, transfer it to the server. The below command will tell that you have saved the Oracle JDK file to your download folder;
scp Downloads/jdk-11.0.7_linux-x64_bin.tar.gz sammy@your_server_ip:~
After uploading to the server, add the third-party repository that assists in installing Oracle’s Java;
The software-properties-common package should be installed. It will add the add-apt-repository on the system;
sudo apt install software-properties-common
Next, to authorize the software, import the signing key;
sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys EA8CACC073C3DB2A
The output you will get;
gpg: key EA8CACC073C3DB2A: public key “Launchpad PPA for Linux Uprising” imported
gpg: Total number processed: 1
gpg: imported: 1
To add the repo to the list of package sources, use the command add-apt-repository;
sudo add-apt-repository ppa:linuxuprising/java
The following message will appear;
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.:
. . .
Then click ENTER to begin the installation. If you get any message related to no valid OpenPGP data found, just ignore it.
For the availability of new software installation, upgrade the package list;
sudo apt update
Then, the installer will search the Oracle JDK in /var/cache/oracle-jdk11-installer-local.
Create the folder with the given name and copy the Oracle JDK 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/
Then, you have to install the package;
sudo apt install oracle-java11-installer-local
While installing, you have to accept the license agreements of Oracle.
To know the selection process of Java’s version, check the details.
Managing Java
Multiple Java can be installed on a single server. To check the default version, use the command below;
sudo update-alternatives –config java
You will get an Output like this if you have installed both versions of 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:
Select the number linked with the Java version to make it as default. And press ENTER.
You can do the same for other java commands;
sudo update-alternatives –config javac
Commands such as, keytool, javadoc, and jarsigner can be used to run the above command.
Arranging the JAVA_HOME environment variable
The JAVA_HOME environment variable is used in various written programs.
You can use the below command;
sudo update-alternatives –config java
Then, you will get to see every command of Java with their directory;
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:
According to the above output, the installations directories are as follow;
Open JSK 11 is installed at /usr/lib/jvm/java-11-openjsk-amd64/bin/hava.
Oracle Java is installed at /usr/lib/jvm/java-11-oracle/jre/bin/java.
From your preferred installation package, copy the installation path and open /etc/environment with nano or any of your preferred text editor;
sudo nano /etc/environment
You have to add the following line at the end of the file. Just keep in mind to replace the highlighted path with the path that you have copied. Just skip the bin/ part of the path;
/etc/environment
JAVA_HOME=”/usr/lib/jvm/java-11-openjdk-amd64″
JAVA_HOME will be set as a path for every user on the system, after the modification of the file.
Finally, Save the file and exit the editor.
To apply changes to the current version, relaunch the file
source /etc/environment
Check whether the environment variable is set;
echo $JAVA_HOME
Then you will get the path as you set it;
/usr/lib/jvm/java-11-openjdk-amd64
Conclusion
In a nutshell, you must have got to learn about the installation process of multiple versions of Java. Moreover, the above mentioned steps also help you in managing java in Ubuntu.
Your Ubuntu is set to run the softwares based on Java like Tomcat, jetty, cassandra, jenkins, and Glassfish.