Installing Java Development Kit 11 on Ubuntu 20.04
Traducciones al EspañolEstamos 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.
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 20.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
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.
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.Ensure your system is up-to-date:
sudo apt-get update && sudo apt-get upgrade
Install OpenJDK
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 for frequent application execution.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 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)
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.
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.Save the changes and exit your text editor.
Reload the
~/.bashrc
file:source ~/.bashrc
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 thePATH
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.
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!"); } }
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.
Compile the application you have written:
javac HelloWorld.java
Confirm that the
HelloWorld.class
file was written to your current directory:ls -l HelloWorld.class
Run the compiled
HelloWorld
function using the JRE:java HelloWorld
The output should again be:
Hello Java World!
This page was originally published on