In the next article we will take a look at how we can install JDK 12 on Ubuntu. Java Development Kit or JDK is a tool for developing Java applications. This will allow users to compile our Java codes, run them, test them and sign them.
Currently we can find 2 versions of JDK. One is called OpenJDK and the other oracle jdk. The first is a project to keep JDK free of Oracle codes. It is an open source implementation of Oracle JDK, which is not open source and has many restrictions.
Install JDK 12 on Ubuntu 19.04
OpenJDK 12 installation
We will be able to find OpenJDK 12 available in the official Ubuntu 19.04 package repository. Therefore, we will be able to install it easily with the APT package manager. First we will have to update the cache of the APT package repository with the following command:
sudo apt update
OpenJDK 12 has two versions. A Complete version and a version of headless system. This latest version does not have GUI programming libraries included and requires less disk space.
If you are interested install the full version of OpenJDK 12, run the following command in the terminal (Ctrl + Alt + T):
sudo apt install openjdk-12-jdk
If you are more interested in install headless system version of OpenJDK 12, the command to execute is the following:
sudo apt install openjdk-12-jdk-headless
After the installation of OpenJDK 12, we can execute the following command to check if OpenJDK is working properly:
java -version
Installing Oracle JDK 12 Using PPA
In Ubuntu 19.04 we will also be able to install Oracle JDK 12. This version of JDK is not available in the official Ubuntu package repository, but we can use the linuxuprising / java PPA to install it.
If we want to add the linuxuprising / java PPA in Ubuntu 19.04, in a terminal (Ctrl + Alt + T) we will only have to execute the command:
sudo add-apt-repository ppa:linuxuprising/java
After this we can install Oracle JDK 12 typing the command:
sudo apt install oracle-java12-installer
During the installation you will have to select “Accept”And press Intro to finish accepting the Oracle Technology Network License Agreement for Oracle Java SE.
After the installation, we can check if it is working by typing the following command in terminal:
java -version
Oracle JDK 12 Installation Using .DEB Package
Another option to install Oracle JDK will download the corresponding .DEB file from the official website. To do so, you will have to visit the Oracle website from the browser. Once on the page you will have to click on the button "Download Java Platform (JDK) 12«.
Tras accept the license agreement, will click the .DEB package file jdk-12.0.1. This is the latest version at the time of writing this article.
The browser will ask us to save the .DEB file. Download finished we will go to the directory ~ / Downloads, or to the folder where you have saved the downloaded package:
cd ~/Descargas
Now we will install the .DEB package as follows:
sudo dpkg -i jdk-12.0.1_linux-x64_bin.deb
The next step to follow will be find the path of the bin / directory of the deb package jdk-12.0.1. We will achieve this with the following command:
dpkg --listfiles jdk-12.0.1 | grep -E '.*/bin$'
Now we will add JAVA_HOME y we will update the PATH variable with the following command:
echo -e 'export JAVA_HOME="/usr/lib/jvm/jdk-12.0.1"\nexport PATH="$PATH:${JAVA_HOME}/bin"' | sudo tee /etc/profile.d/jdk12.sh
After this, we have reboot our Ubuntu machine with the following command:
sudo reboot now
Once the computer restarts, we can run the following command to check if JAVA_HOME and PATH environment variables are set correctly:
echo $JAVA_HOME && echo $PATH
If everything is correct, we can check if Oracle JDK 12 works correctly typing:
java -version
Compiling and Running a Simple Java Program
Once the JDK 12 is installed, the next step will be to write a small and simple Java program to check if we can compile it and run it with OpenJDK 12 or Oracle JDK 12.
To do it we will create a file called TestJava.java and inside we will write the following lines:
public class PruebaJava { public static void main(String[] args) { System.out.println("Hola usuarios Ubunlog"); } }
Now for compile the source file TestJava.java In a terminal (Ctrl + Alt + T) we will go to the directory where the file we just created is saved. In this folder we execute the following command:
javac PruebaJava.java
This command should generate a new file called TestJava.class. This is a Java class file and contains Java bytecodes that the JVM (Java VirtualMachine) can execute.
If everything has been correct, we can run the Java class file TestJava.class As follows:
java PruebaJava
In the previous command you have to write only the file name without the .class extension. Otherwise it won't work. If all goes well, we will see the expected exit. Therefore, the JavaTest.java program compiled and ran successfully using JDK 12.
Thank you very much, the guide helped me