Introduction
Java is one of the most popular programming languages. It is used for developing anything from lightweight mobile to desktop applications.
This step-by-step guide will show you how simple it is to install Java on Ubuntu 18.04 or 20.04.
Note: This guide provides instructions that work on Ubuntu 18.04, Ubuntu 20.04 and any other Ubuntu-based distribution (including Linux Mint, Kubuntu, and Elementary OS). If you are looking for other Java installation guides, please refer to:
- Installing Java on Fedora
- Installing Java on CentOS 7 or CentOS 8
- Installing Java on Raspberry Pi
Prerequisites
- A user account with sudo privileges
- Access to the command-line/terminal window
- The apt (apt-get) command
Java Versions
There are four Java platforms available:
- Java Standard Edition (Java SE)
- Java Micro Editions (Java ME)
- Java Enterprise Edition (Java EE)
- JavaFX
In this document, we look at different packages within the Java SE.
The Ubuntu repository offers two (2), open-source Java packages, Java Development Kit (Open JDK) and Java Runtime Environment (Open JRE). You use JRE for running Java-based applications, while JDK is for developing and programming with Java.
Also available is Oracle Java, another SE implementation, which has additional commercial features. You can find the official Oracle JDK through a third-party repository. However, bear in mind the license only allows non-commercial use of the software.
Installing Java on Ubuntu
You can install one or several Java packages. You can also decide which version you want on your system by installing a specific version number. The current default and LTS version is Java 11.
Install OpenJDK
1. Open the terminal (Ctrl+Alt+T) and update the package repository to ensure you download the latest software version:
sudo apt update
2. Then, you can confidently install the latest Java Development Kit with the following command:
sudo apt install default-jdk
3. Confirm the installation by typing y (yes) and press Enter.
Install OpenJRE 11
1. Before you install the Java Runtime Environment, make sure you update the repository with:
sudo apt update
2. Next, type in the following command to download and install Java RE:
sudo apt install default-jre
Type y (yes) and press Enter to confirm the installation.
Install Oracle Java 11
To download the official Oracle JDK, you first need to download a third-party repository.
We include instructions for installations from 2 (two) different package repositories. You can decide from which one you prefer to download.
Option 1: Download Oracle Java from Webupd8 PPA
1. First, add the required package repository by typing:
sudo add-apt-repository ppa:webupd8team/java
Hit Enter when prompted.
2. Make sure to update your system before initiating any installation:
sudo apt update
3. Now, you can install Java 11, the latest LTS version:
sudo apt install oracle-java11-installer
4. Optionally, you can set this Java version as the default with the following command:
sudo apt install oracle-java11-set-default
Option 2: Download Oracle Java from Linux Uprising PPA
1. Before adding the new repository, install the required packages if you do not have them on your system yet:
sudo apt install software-properties-common
2. Next, add the repository with the following command:
sudo add-apt-repository ppa:linuxuprising/java
3. Update the package list before installing any new software with:
sudo apt update
4. Then, download and install the latest version of Oracle Java (version number 11):
sudo apt install oracle-java11-installer
Verify Java is Installed on Your System
Now verify whether the software is part of your system.
To check the version of Java running, use the command:
java –version
The successful installation of Java on Ubuntu will output details, as in the image below.
If there is no Java version, the system will say it is unable to find the software.
Install Specific Version of Java
If for some reason you do not wish to install the default or latest version of Java, you can specify the version number you prefer.
Install Specific Version of OpenJDK
You may decide to use Open JDK 8, instead of the default OpenJDK 11.
To do so, open the terminal and type in the following command:
sudo apt install openjdk-8-jdk
Verify the version of java installed with the command:
java –version
Install Specific Version of Oracle Java
When you download the Oracle Java packages from a third-party repository, you have to type out the version number as part of the code.
Therefore, if you want other versions of Java Oracle on your system, change that number accordingly.
The command for installing Oracle JDK is the following (the symbol # representing the Java version):
sudo apt install oracle-java#-installer
For instance, if you want to install Java 10, use the command:
sudo apt install oracle-java10-installer
How to Set Default Java Version
As you can have multiple versions of Java installed on your system, you can decide which one is the default one.
First, run a command that shows all the installed versions on your computer:
sudo update-alternatives --config java
The image above shows that there are two alternatives on this system. These choices are represented by numbers 1 (Java 11) and 2 (Java 8), while the 0 refers to the current default version.
As the output instructs, you can change the default version if you type its associated number (in this case, 1 or 2) and press Enter.
How to Set JAVA_HOME Environment Variable
The JAVA_HOME environment variable determines the location of your Java installation. The variable helps other applications access Java’s installation path easily.
1. To set up the JAVA_HOME variable, you first need to find where Java is installed. Use the following command to locate it:
sudo update-alternatives --config java
The Path section shows the locations, which are in this case:
- /usr/lib/jvm/java-11-openjdk-amd64/bin/java (where OpenJDK 11 is located)
- /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java (where OpenJDK 8 is located)
2. Once you see all the paths, copy one of your preferred Java version.
3. Then, open the file /etc/environment with any text editor. In this example, we use Nano:
nano /etc/environment
4. At the end of the file, add a line which specifies the location of JAVA_HOME in the following manner:
JAVA_HOME=”/your/installation/path/”
For example, if we copied the installation path for Java 11 in the previous step, the added line in the text code editor would be:
How to Uninstall Java on Ubuntu
In case you need to remove any of the Java packages installed, use the apt remove
command.
To remove Open JDK 11, run the command:
sudo apt remove default-jdk
To uninstall OpenJDK 8:
sudo apt remove openjdk-8-jdk
Once you run the command, confirm the continuation of the process by pressing y (yes) and the packages will be removed.
Note: Refer to our comprehensive breakdown of Best Java IDEs to find the best one for your needs.
Conclusion
This guide has shown you how to install Java on Ubuntu 18.04. It also instructed you on how to change the default version and set the home environment variable.
Now, you are ready to use the powerful Java platform!
For more tutorials on how to install Java on other systems, make sure to read our article on how to install Java on Windows.
原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/222893.html