Search
Close this search box.

How to install the Java Development Kit with apt and without apt in Ubuntu 18.04

Table of Contents

Tutorial on installing the Java Development Kit with apt and without apt in Ubuntu 18.04

A comprehensive guide for Java Development Kit

Java is one of the world’s most widely used programming languages, and the Java Development Kit (JDK) is the software development kit (SDK) used by Java developers to create applications. The JDK includes tools such as a compiler, debugger, and other utilities essential for Java development. This article will look in-depth at the Java Development Kit, its features, use cases, advantages, disadvantages, alternatives, and more.


What is Java Development Kit?


The Java Development Kit (JDK) is a software development kit used by Java developers to create Java applications. It includes the Java Runtime Environment (JRE), a compiler, a debugger, and other utilities essential for Java development. The JDK is available for multiple platforms, including Windows, Linux, and macOS.

 

Installing the Default JRE/JDK

The easiest option for installing Java is to use the version packaged with Ubuntu. By default, Ubuntu 18.04 includes OpenJDK version 11, which is an open-source variant of the JRE and JDK.

To install this version, first, update the package index:

sudo apt update

Next, check if Java is already installed:

java -version

If Java is not currently installed, you’ll see the following output:

Output
Command 'java' not found, but can be installed with:

apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headless

Execute 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.

Verify the installation with:

java -version

You’ll see the following output:

Output
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu218.04)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu218.04, 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. To install the JDK, execute the following command, 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

You’ll see the following output:

Output
javac 11.0.7

Next, let’s look at how to install Oracle’s official JDK and JRE.

 

Installing Oracle JDK 11

 

Oracle’s licensing agreement for Java doesn’t allow automatic installation through package managers. To install the Oracle JDK, which is the official version distributed by Oracle, 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. Then you can use apt to install it with help from a third-party installation script.

To download the JDK 11 visit this page and download the Linux Compressed Archive file.

Once the file has been downloaded, you have to upload it to your server.

Once the file upload has been completed, you are ready to install JDK 11.

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

sudo apt install software-properties-common

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

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A

You’ll see this output:

Output
gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" imported
gpg: Total number processed: 1
gpg: imported: 1

Then use the add-apt-repository command to add the repo to your list of package sources:

sudo add-apt-repository ppa:linuxuprising/java

You’ll see this message:

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

Press ENTER to continue the installation. You may 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.8_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/

Finally, install the package:

sudo apt install oracle-java11-installer-local

The installer will first ask you to accept the Oracle license agreement. Accept the agreement, then the installer will extract the Java package and install it.

Now let’s look at how to select which version of Java you want to use.

 

Managing Java

You can have multiple Java installations on one server. You can configure which version is the default for use on the command line by using the update-alternatives command.

sudo update-alternatives --config java

This is what the output would look like if you’ve installed both versions of Java in this tutorial:

Output
There are 2 choices for 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

Choose the number associated with the Java version to use it as the default, or press ENTER to leave the current settings in place.
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 the key tool, Javadoc, and jarsigner.

 

Setting the JAVA_HOME Environment Variable

Many programs 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. Use the update-alternatives command:

sudo update-alternatives --config java

This command shows each installation of Java along with its installation path:

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:
OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
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:

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

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

Save the file and exit the editor.

Now reload this file to apply the changes to your current session:

source /etc/environment

Verify that the environment variable is set:

echo $JAVA_HOME

You’ll see the path you just set:

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

Other users will need to execute the command source /etc/environment or log out and log back in to apply this setting.

 

JDK Compared to Other Systems:

The JDK is one of the world’s most widely used software development kits, and it is well-known for its robustness and versatility. Compared to other systems, the JDK offers a wide range of features that make it a popular choice for Java developers. Some of these features include:

  1. Cross-platform compatibility: The JDK is available for multiple platforms, including Windows, Linux, and macOS, making it a versatile choice for developers who work on different platforms.
  2. Rich libraries: The JDK includes a vast array of libraries that developers can use to build applications, making it easier to create complex applications quickly.
  3. Powerful debugging tools: The JDK includes a powerful debugger that makes it easier to find and fix bugs in code.
  4. Wide range of tools: The JDK includes tools that can help developers improve productivity, including a profiler, a Javadoc generator, and more.

 

Use Cases Of JDK:

Java developers use the JDK to create various applications, from simple desktop applications to complex enterprise-level applications. Some of the most common use cases for the JDK include the following:

  1. Desktop applications: The JDK can create Windows, Linux, and macOS desktop applications.
  2. Mobile applications: The JDK can create Android applications using the Android Studio IDE.
  3. The JDK can create web applications using frameworks such as Spring and Hibernate.
  4. Enterprise applications: The JDK is often used to create large-scale enterprise applications that require high performance and reliability.

 

Features Of JDK:

The JDK includes many features, making it a powerful tool for Java developers. Some of the most important features include the following:

  1. Cross-platform compatibility: The JDK is available for multiple platforms, including Windows, Linux, and macOS.
  2. Rich libraries: The JDK includes a vast array of libraries that developers can use to build applications, making it easier to create complex applications quickly.
  3. Powerful debugging tools: The JDK includes a powerful debugger that makes it easier to find and fix bugs in code.
  4. Wide range of tools: The JDK includes tools that can help developers improve productivity, including a profiler, a Javadoc generator, and more.
  5. Scalability: The JDK is highly scalable and can create applications that can handle millions of users.

 

Advantages Of JDK :

The JDK offers several advantages to Java developers. Some of these advantages include the following:

  1. Cross-platform compatibility: The JDK is available for multiple platforms, including Windows, Linux, and macOS, making it a versatile choice for developers who work on different platforms.
  2. Rich libraries: The JDK includes a vast array of libraries that developers can use to build applications, making it easier to create complex applications quickly.
  3. Powerful debugging tools: The JDK includes a powerful debugger that makes it easier to find and fix bugs in code.
  4. Wide range of tools: The JDK includes tools that can help developers improve productivity, such as a profiler, a Javadoc generator, and more.
  5. Scalability: The JDK is highly scalable and can create applications that can handle millions of users.
  6. Community support: Java has a large and active community of developers who share knowledge and support each other.

Disadvantages Of JDK:

While the JDK offers several advantages, there are also some disadvantages to using it. Some of these disadvantages include the following:

  1. Steep learning curve: Java can be difficult to learn, especially for beginners, due to its complexity and the many libraries and tools available.
  2. Performance: Java can be slower than other programming languages, especially for applications that require high performance.
  3. Memory usage: Java applications can be memory-intensive, disadvantaging applications that must run on devices with limited memory.
  4. Security issues: Java has had some security issues, which can concern developers who need to create secure applications.

 

Alternatives Of JDK:

While the JDK is the most widely used software development kit for Java, there are also some alternatives that developers can consider. Some of these alternatives include:

  1. Eclipse: Eclipse is an open-source integrated development environment (IDE) that supports multiple programming languages, including Java.
  2. NetBeans: NetBeans is an open-source IDE designed specifically for Java development.
  3. IntelliJ IDEA: IntelliJ IDEA is a commercial IDE that supports multiple programming languages, including Java.

 

Conclusion:

The Java Development Kit (JDK) is a powerful tool for Java developers, offering a wide range of features, libraries, and tools that make it easier to create complex applications quickly. While the JDK has some disadvantages, such as its steep learning curve and performance issues, its advantages, such as cross-platform compatibility and community support, make it a popular choice for Java developers.

 

Additionally, while there are some alternatives to the JDK, it remains the most widely used software development kit for Java and will likely remain so for the foreseeable future.

 

 

Tips and tricks

In this tutorial, you have successfully learned how to install the Java Development Kit with apt and without apt in Ubuntu 18.04.

If you want a server with Ubuntu 18.04 operating system, get started now with VPSie and get one month for free.

This tutorial is to help you based on this original tutorial please use our tutorial for notes and tips.

 

Windows (Server) VPS hosting

 

10 factors to decide on the best cloud or SSD VPS hosting provider

Java advancement Kit (JDK) is a software advancement environment for building applications and applets using the Java programming language. It includes a compiler, runtime environment, libraries, and tools needed for Java development.

apt (Advanced Package Tool) is a package management system used in Ubuntu and other Debian-based Linux distributions. It allows users to install, update, and remove software packages from a central repository.

To install JDK with apt in Ubuntu, follow these steps:

  1. Open a terminal window
  2. Update the package list using the command: sudo apt-get update
  3. Install the default JDK using the command: sudo apt-get install default-jdk
  4. Verify the initiation using the command: java -version

To install JDK without apt in Ubuntu, follow these steps:

  1. Download the JDK from the Oracle website (https://www.oracle.com/java/technologies/javase-jdk16-downloads.html)
  2. Extract the downloaded archive using the command: tar -xvf <filename>
  3. Move the extracted folder to /usr/local using the command: sudo mv <foldername> /usr/local/
  4. Set the JAVA_HOME environment variable using the command: export JAVA_HOME=/usr/local/<foldername>
  5. Add the JDK bin register to the PATH environment variable using the command: export PATH=$PATH:$JAVA_HOME/bin
  6. Verify the initiation using the command: java -version

Using apt to install JDK in Ubuntu has several advantages, including:

  1. Easy installation process with a few simple commands
  2. Automatic updates and security patches
  3. Ability to manage dependencies and package conflicts
  4. Centralized package management system for easy software management

 To uninstall JDK installed with apt in Ubuntu, follow these steps:

  1. Open a terminal window
  2. Remove the JDK package using the command: sudo apt-get remove default-jdk
  3. Remove any remaining configuration files using the command: sudo apt-get purge default-jdk
  4. Verify that JDK is uninstalled using the command: java -version

 

Make a Comment
Share on
Facebook
Twitter
LinkedIn
Print
VPSie Cloud service

Fast and Secure Cloud VPS Service

Try FREE
For a month

The First 1 orders gets free discount today! Try Sign up on VPSie to get a chance to get the discount.