How to install Java in Ubuntu

You are currently viewing How to install Java in Ubuntu
How To Install Java In Ubuntu

The steps for how to install Java in Ubuntu, depends upon your choice of JDK type. There are 2 choices Oracle JDK and OpenJDK. JDK means the Java development kit, which sometimes people just refer as Java. Instead of JDK, you can also choose to install JRE. The difference between JDK and JRE is explained here as well. In this article we are going to discuss the detailed steps to install java in Ubuntu using terminal window.

Install Java in Ubuntu as packages from terminal window, Oracle JDK or OpenJDK are the 2 popular choices for Java installation. The command “apt” is used for installation of packages for corresponding repositories. After installation, use the command “Java -version” to verify the installation.

What is java language?

Java is a object-oriented and class-based programming language, it is designed to have as minimum possible implementation dependencies. Java programming is a general-purpose programming language, intended to let programmers write once, run anywhere (WORA). It means the compiled Java code can be run on all those platforms which support Java and there is no need for recompilation.

What are java editions?

Java editions are basically Java platforms. Java technology is both a programming language as well as a platform. There are total four editions/platforms of the Java programming language:

  1. Java SE (Java Platform, Standard Edition): Java SE provides the basic API for the core functionality of the Java programming. Java SE defines everything from the essential types and objects of the Java programing language to high-level classes that are used for networking, security, database access, graphical interface (GUI) development, and XML parsing. Along with the core API, the Java SE platform includes a Java virtual machine, development technologies, deployment tools, and other important toolkit & class libraries commonly utilized in Java technology applications.
  2. Java EE (Java Platform, Enterprise Edition): The Java EE platform is actually built on top of Java SE platform. The Java EE platform provides us an API and runtime environment (JRE) for developing & running multi-tiered, reliable, large-scale and secure network applications.
  3. Java ME (Java Platform, Micro Edition): The Java ME platform provides us an API as well as a small-footprint virtual machine for running the Java programming language applications on small electronics, like mobile phones. This API is the subset of Java SE API, along with the special class libraries, which are useful for application development for small device. Java ME applications are more oftenly clients of Java EE platform services.
  4. JavaFX: JavaFX is a basically a platform for creating rich look internet applications using a lightweight user-interface API. JavaFX applications use hardware-accelerated graphics and media engines to require advantage of higher-performance clients and a contemporary look-and-feel also as high-level APIs for connecting to networked data sources.

What is difference between JDK, JRE and JVM?

JDK is a software development kit which is called as Java Development Kit or JDK. JDK installation is platform dependent and can be install using the platform specific installer. JDK contains tools for development, debugging, etc.
JRE is Java Runtime Environment, which is a software bundle that allows Java program to run. JRE is also platform dependent and contains class libraries and other supporting files, which provides the environment to execute source code.
JVM is a Java Virtual Machine, which is an environment for executing the bytecode. JVM dont include software development tools, but JVM itself bundled in both JDK and JRE. JVM is platform independent.

OpenJDK vs Oracle JDK

Oracle JDK is the JDK provided from Oracle Company with commercial license and you are bound to purchase a license if you want to use it for commercial purposes. OpenJDK is an open-source implementation of the Java Platform – Standard Edition, which is supported by community. OpenJDK is available under GNU license which means its free for commercial purposes as well. Most popular versions are OpenJDK-8 and OpenJDK-11 on Ubuntu.

In 2019, Oracle announced that they would start charging for Java SE licenses for new versions and updates, after Java 11. Since then, many companies are either need to purchase a commercial license (from Oracle) in order to receive software updates or switch to OpenJDK.
The biggest difference in Oracle JDK vs OpenJDK is type of license. OpenJDK is completely open source Java, free for commercial purpose while Oracle JDK requires a commercial license.

Install java in Ubuntu using terminal

Install Java in Ubuntu using terminal window or command line. Open the terminal using shortcut (Ctrl+Alt+T). Before starting installation, make sure you have sudo user rights. You can install jre on Ubuntu if you dont want to install JDK.

How to install OpenJDK in Ubuntu?

The easiest way to install the OpenJDK is to use the default-jdk version, it will automatically install the latest available version of OpenJDK. Follow these steps to install OpenJDK on Ubuntu:

1. Make sure you have the Superuser rights on Ubuntu, otherwise installation will be failed due to user permissions.

E:Could not open lock file /var/lib/dpkg/lock-frontend - open (13:Permission denied)
E:Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend),are you root?

2. To install the latest version, first update the package index using “apt”

sudo apt update

3. Once the update to package index is complete, start installing the JDK

sudo apt install default-jdk

4. JDK itself contains JRE, if you want to only install JRE on Ubuntu then use following command

sudo apt install default-jre

5. Check Java version to verify the installation

java -version

You will get this type of Output:

openjdk version "11.0.8"  2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1,mixed mode,sharing)

If you see above type of output, version may get changed with time. Then you have successfully installed the OpenJDK.

Install OpenJDK specific version

You can install specific OpenJDK. Type “sudo apt install openjdk-” and press Tab button to see available options. For example to install JDK 8 or JDK 11, you can use 1 of following command.

sudo apt install openjdk-8-jdk
sudo apt install openjdk-11-jdk

How to install Oracle JDK in Ubuntu?

Before installing Oracle JDK, make your you read the license terms of Oracle JDK. Oracle JDK is free for non-commerical use only, like personal use, education and development. Follow these steps to install Oracle JDK in Ubuntu:

1. Install the software-properties-common package first, it adds the add-apt-repository command to the system.

sudo apt install software-properties-common

2. Now use the add-apt-repository command to include the repo to your machine list of package sources.

sudo add-apt-repository ppa:linuxuprising/java

3. Update now your package list to include the new software available for installation on machine.

You can use any available version of Oracle Jdk from version 7 to version 15 (available at the time of writing this article). I am using JDK 8 here to simplify the process.

sudo apt update
sudo apt install oracle-java8-installer

You will be asked to accept license and continue the installation.

4. Verify you installation using the command

java -version

Your output will look like this

java version  "1.8.0_201"
Java(TM) SE Runtime Environment (build  1.8.0_201-b09)
Java HotSpot(TM)  64-Bit Server VM(build 25.201-b09,mixed mode)

How to set default java version in Ubuntu?

You can have multiple installed versions of JDK on one server. You can configure at any time which version is the default for use from the command line. You can use the update-alternatives command for switching betweek JDk versions.

sudo update-alternatives --config java

You will see the list of available installed versions of Java.

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      1111      auto mode
* 1        /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      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

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

You need to choose the number of Java version from the list and press Enter key and your default version will be changed, which you can again verify using the command “java -version”.

How to uninstall Java from Ubuntu?

You can uninstall Oracle JDK from Ubuntu using following command, you can change the version which you want to remove:

sudo apt-get remove --purge oracle-java8-installer

You can uninstall OpenJDk from Ubuntu just like a normal package removal, Use following command with specific version you want to remove:

sudo apt remove openjdk-8-jdk


So these were the steps which can guide you whenever you thought How To Install Java In Ubuntu.

This Post Has 3 Comments

Comments are closed.