Home Java How to Install Latest Java and Set JAVA_HOME on Ubuntu

How to Install Latest Java and Set JAVA_HOME on Ubuntu

Published: Last Updated on 1 comment

Hey, Tea lovers! In this post, we will have a look at how we can easily install the latest Java or JDK on the Ubuntu system. It will be a very quick and small post.


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


Before You Start

I will be installing Open JDK, and I will show the installation for JDK 8, JDK 11, and JDK 14 on Ubuntu. For the Windows user, I have already written a post on “Install Latest or Any Java Version in 3 Simple Steps: Windows“.

As you might know, for us developers to develop Java programs, we need JDK and not only JRE. If you install JRE then you can only run the Java programs and not compile them. So keep in mind, whenever I say Java, in this post, I will be referring to the JDK.

Before installing make sure to update the repositories with the following.

sudo apt-get update && sudo apt-get upgrade -yCode language: JavaScript (javascript)

Install Java 8 on Ubuntu

To install Java 8 in Ubuntu, you need to run the following command.

sudo apt-get install openjdk-8-jdkCode language: JavaScript (javascript)

It will ask for confirmation, simply type and press y , and hit enter. Then wait for a few minutes until it gets installed.

How to get the JAVA_HOME path for Java 8 is described in the last block of this post. Click here to jump to the block.

Install Java 11 on Ubuntu

For Java 11 the process is the same. Simply change the version to 11. And you will get the following command to install Java 11 on Ubuntu.

sudo apt-get install openjdk-11-jdkCode language: JavaScript (javascript)

The same confirmation and waiting time.

The JAVA_HOME path for Java 11 is shown in the last block of this post. Click here to jump to the block.

Install Java 14 on Ubuntu

Until now, you guys must have realized that you simply need to change the number in the command openjdk-<version-number>_jdk, and you can install the desired version. But just for the sake of SEO, let me add one more block for Java 14 as well. The command to install Java 14 on Ubuntu is as follows. #

sudo apt-get install openjdk-11-jdkCode language: JavaScript (javascript)

See the next block to see how to get the JAVA_HOME path for Java 14.

Set Java Home Variable JAVA_HOME in Ubuntu

After installing Java on any machine, you must set up the JAVA_HOME. Without JAVA_HOME many of the tools or packages will not work, such as Maven. To set up the path you need to figure out the path of the installed Java on your machine.

Get the List of Installed Java in your Ubuntu Machine

The best way to see the installed Java path is to use the following command.

update-java-alternatives --listCode language: PHP (php)

This command will show you the list of installed Java on your Ubuntu machine. Me, I have installed both Java 11 and Java 8, so I will be seeing two outputs as follows.

java-1.11.0-openjdk-amd64 1111  /usr/lib/jvm/java-1.11.0-openjdk-amd64
java-1.8.0-openjdk-amd64  1081  /usr/lib/jvm/java-1.8.0-openjdk-amd64

The first column indicated the java version and the 3rd column is the respected path for that Java. So Java 11, is installed on /usr/lib/jvm/java-1.11.0-openjdk-amd64 and For java 8 it got installed on /usr/lib/jvm/java-1.8.0-openjdk-amd64.

Set up JAVA_HOME

Go to Terminal and type the following command.

gedit ~/.bashrc

It will open the .bashrc file in the text editor. Go to the end of the file and type the following command. I will be using the path of Java 11 but you can use whatever Java you have installed in your ubuntu.

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64Code language: JavaScript (javascript)

Replace the path after = with your desired Java path.

Setup JAVA_HOME for current Java

Alternatively, you can hassle-free update the JAVA_HOME with the following command. Just do this, and forget about setting up JAVA_HOME each time you install the latest version.

export JAVA_HOME=$(dirname $(dirname $(realpath $(which javac))))Code language: Bash (bash)

Conclusion

That’s it for this post. We looked at how we can easily install and set up any version of Java. I hope you liked the post. I will be soon publishing one for the centos as well, so stay connected with us on social media @coderstea.

See you in the next post.

HAKUNA MATATA!!!


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ads
Ads
Ads

@2023 All Right Reserved. Designed and Developed by CodersTea

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More