In the previous posts, we have discussed how to install Java 10, and Java 11 on Ubuntu. We will continue with the same and install Java 12 on Ubuntu 18.04 LTS (Bionic Beaver) in this post. The steps should be the same for other versions of Ubuntu, and Linux.
Important Notes: Oracle has already announced that the Java updates and new releases available on its official site won't be free from 16th April 2019 onwards. The consumer has to purchase a commercial license unless and otherwise Java is used for personal and development purpose. We can still use Java for personal, development, and commercial usage without purchasing any license by using the OpenJDK distributed by Oracle under the GNU General Public License, version 2, with the Classpath Exception, and available on http://jdk.java.net.
This tutorial covers the installation of OpenJDK on Ubuntu 18.04 LTS.
Notes: JDK 13 is about to release on 17th Sept 2019. You may follow What's New In Java 13 to know about the new features introduced in JDK 13. You can also follow How To Install OpenJDK 13 On Windows and How To Install OpenJDK 13 On Ubuntu to install OpenJDK 13 and How To Install Java 13 On Windows to install Oracle JDK.
Open the JDK 12 GA Release site and download the distribution for Linux systems as shown in Fig 1.
Open the terminal and make the directory /usr/java to keep all the java installations at the same place. We can install multiple versions of Java in this directory. Now copy the downloaded file to this location and extract it as shown below:
>sudo mkdir /usr/java >cd /usr/java >sudo cp /m1data/setups/openjdk-12.0.1_linux-x64_bin.tar.gz openjdk-12.0.1_linux-x64_bin.tar.gz >sudo tar -xzvf openjdk-12.0.1_linux-x64_bin.tar.gz
The above steps will install JDK to the path /usr/java/jdk-12.0.1.
Set Environment Variables
In this step, we will configure the environment variable to use the JDK installed by us.
>sudo nano /etc/profile
Scroll down by pressing Page Down button and add at the end of this file:
JAVA_HOME=/usr/java/jdk-12.0.1 PATH=$PATH:$HOME/bin:$JAVA_HOME/bin export JAVA_HOME export JRE_HOME export PATH
Now press Ctrl + O and press Enter to write our change. Press Ctrl + X to exit the nano editor. The nano editor should look like the one shown in Fig. 2.
Configure Java Commands
We can configure the Java commands to use the newly installed JDK by default. We can check the installed Java before and after executing these commands as shown below:
>java -version >sudo update-alternatives --install "/usr/bin/java" "java" "/usr/java/jdk-12.0.1/bin/java" 1 >sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/jdk-12.0.1/bin/javac" 1 >java -version
The output of these commands is shown in Fig. 3. We might need to configure active Java if it is previously installed on the system.
These are the basic steps required to install the most recent version of Java on Ubuntu.
In this section, we will write our first program in Java using the nano editor.us.
>sudo mkdir /m1data/programs
>sudo nano HelloJava.java
Now write the first Java program as shown in Fig 4, save the program and exit the editor.
Use the javac and java to compile and execute the program as shown below.
// Compile the program
sudo javac HelloWorld.java
// Execute the program
sudo java HelloWorld
// Program output
Hello Java !!
Thee are the basic steps to write Java programs.