OpenJDK is an open source version of Java Development Kit and is available on most Linux package repositories. Sadly, the version is quite outdated since Java programs today are compiled for the latest Java Environment (which is now version 10), the package found on most Linux repositories is still in previous stable release ie., v1.8. This article will guide you on how to install the latest OpenJDK on your Linux computer, and how to set its environment variables so you can start right away with your Java project.
To download OpenJDK head over to the website jdk and click on the “Ready for use” link.
Under Downloads heading, select the Linux tar.gz link. Now, this is nothing like the deb or rpm file you normally download to install a Linux program on your computer. However, it has an advantage and that is such tarballs are reliable for distributing Linux software across multiple Linux distributions since it would work on almost all of them.
The only disadvantage is you would have to manually access the website and download OpenJDK every time a new Java version is released. But fret not, you still enjoy the same stable version for a period of more than six months. So it’s not going to be rapidly updated as Firefox and Google Chrome do.
If you are cautious of what you download from the Internet, make sure you click that sha256 link and verify it with the Linux tarball you just downloaded.
Set up OpenJDK
Now it’s time to unpack the archive file and install OpenJDK on your computer. There are two ways to extract the tarball:
- Using a File manager program or
- Using a terminal program
Since the graphical method is easier on most modern Desktop Environments, I’ll proceed ahead with the File Manager program. Browse to the Downloads directory/library and right-click the OpenJDK tarball file, you’ll notice Extract (Extract to or Extract here) options. Extract the file to your home root directory, not on the Downloads directory.
Take note that, the contents are inside the folder named “jdk-10.0.1”. So if by mistake the tarball was extracted to Downloads directory, move the folder ie., jdk-10.0.1, to your home directory. And that’s it! You’ve successfully installed OpenJDK on your computer. Now all you have to do is set up its environment variables.
Set up environment variables
Since we’re installing the OpenJDK program on one specific user account, we’ll set up environment variables as local (only accessible from your user account). Otherwise, if you intended other users to access the program you’d have to move the jdk folder to another partition and mount the partition automatically when your computer boots up by editing the /etc/fstab file.
For single user account (local)
Edit the file ~/.profile using one of your favorite text editors and add the below commands on the last line.
export JAVA_HOME="~/jdk-10.0.1" export JDK_HOME="~/jdk-10.0.1" export PATH="$JAVA_HOME/bin:$PATH"
You can add descriptive comments to remind yourself of its purpose.
For all user accounts (system-wide)
Using sudo, create a file named openjdk.sh on /etc/profile.d/ directory and enter the below commands to the new file.
export JAVA_HOME="/jdk-10.0.1" export JDK_HOME="/jdk-10.0.1" export PATH="$JAVA_HOME/bin:$PATH"
After you’re done, log out from your account and log back in so the environment variables get updated. Then launch a terminal program and test the java version to make sure it’s ready to be used.
To uninstall OpenJDK, simply delete its folder and undo the environment variables setup by removing those three export lines. However, if you set it as system-wide delete that openjdk.sh file too.