Installing OpenJDK 11 (Java Development Kit) on Ubuntu 18.04

Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

Java is one of the world’s most popular programming languages. Software written in Java can be compiled and run on any system, making Java a versatile platform that can be used to create anything from software to basic web applications. This guide will show you how to install the Open Java Development Kit (OpenJDK) 11 on Ubuntu 18.04.

OpenJDK is the free and open-source implementation of the Oracle Java Standard Edition (Java SE) Development Kit. OpenJDK and Java SE are equivalent JDKs that include a Java runtime environment (JRE) and tools for developing and compiling Java applications.

While there are many available versions of OpenJDK, version 11 is the latest Long-Term-Support (LTS) release as of the time of this guide’s publication. For this reason, OpenJDK 11 is the recommended version for developing production applications.

Before You Begin

  1. Familiarize yourself with our Getting Started guide and complete the steps for connecting to your Linode with SSH and setting your Linode’s hostname and timezone.

  2. Complete the sections of our guide on Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services. This guide will use sudo commands wherever possible, which should be run by a limited, non-root user on your Linode.

  3. Ensure your system is up-to-date:

    sudo apt-get update && sudo apt-get upgrade
    

Install OpenJDK

  1. Install the OpenJDK 11 development kit, which includes OpenJRE 11:

    sudo apt-get install openjdk-11-jdk
    

    Alternatively, if you simply want to run Java applications that you have already downloaded, you can choose to only install OpenJRE 11:

    sudo apt-get install openjdk-11-jre
    
    Note
    While you can run Java applications directly with the JRE, your applications will be compiled every time they are executed. This is generally slower than running applications that have already been compiled into Java bytecode, and may not be suitable if you plan to execute applications many times.
  2. Check the version of the JRE to verify that it has been properly installed:

    java -version
    

    As of the time of this publication, this command should return:

    openjdk version "11.0.7" 2020-04-14 LTS
    OpenJDK Runtime Environment 18.9 (build 11.0.7+10-LTS)
    OpenJDK 64-Bit Server VM 18.9 (build 11.0.7+10-LTS, mixed mode, sharing)
  3. If you have chosen to install the full OpenJDK development kit, check the version of the compiler as well:

    javac -version
    

    As of the time of this publication, this command should return:

    javac 11.0.7

Set Environment Variables

This section will instruct you on how to set the JAVA_HOME and PATH environment variables to help ensure that your Java applications will run without issue.

  1. Open the ~/.bashrc startup file using the text editor of your choice and add the following definitions at the end of the file:

    File: ~/.bashrc
    1
    2
    3
    
    # [...]
    export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
    export PATH=$PATH:$JAVA_HOME/bin
    Note
    If you are using a shell other than Bash, such as Zsh, you may need to add these lines in a different startup file instead. In the case of Zsh, this would be the ~/.zshrc file.
  2. Save the changes and exit your text editor.

  3. Reload the ~/.bashrc file:

    source ~/.bashrc
    
  4. Verify that the JAVA_HOME and PATH variables were set correctly:

    echo $JAVA_HOME
    echo $PATH
    

    The JAVA_HOME variable should be set to the directory that contains your OpenJDK installation, and the PATH variable should include the directory that contains the OpenJDK binary files.

Test the Java Installation (Optional)

To test your Java installation, write a sample HelloWorld Java application and run it with the JRE.

  1. Open a text editor and add the following lines in a file labeled HelloWorld.java to create a simple function that prints “Hello Java World!”:

    File: HelloWorld.java
    1
    2
    3
    4
    5
    
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello Java World!");
        }
    }
  2. Run the application using the JRE:

    java HelloWorld.java
    

    If the installation has been successful, the output will be:

    Hello Java World!

If you have installed the full OpenJDK development kit, you can compile your application into a bytecode class file prior to running it for faster execution time.

  1. Compile the application you have written:

    javac HelloWorld.java
    
  2. Confirm that the HelloWorld.class file was written to your current directory:

    ls -l HelloWorld.class
    
  3. Run the compiled HelloWorld function using the JRE:

    java HelloWorld
    

    The output should again be:

    Hello Java World!

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.