Installing Open JDK on Ubundu

Introduction

Here we come with another Java cool stuff. Ever wondered how to keep yourself upto date with Java's new release structure. I am going to talk on how to keep yourself upto date with Java, JDK on your Ubundu/Deb flavored Linux that you love.

I personally use Linux Mint and was struggling to have a nice way to keep up with the new versions of Java and had written multiple scripts to automate the upgrades and stay current. When I found that the feature of using Java alternatives can be the right tool I did do some investigation and found that that is the best way I have found sofar that is working. Hence, I decided to quickly make some notes for myself then thought of putting it as a blog so that I could potentially share with my friends and to the community.

How To: Install Open JDK.

Prerequisites

Here are somethings that you will require,

  1. A system with Ubundu/Deb flavored Linux
  2. You will have to have root access
  3. Some disk space to install the software of course.

Step 1: Adding Source

Now you will have to add the PPA for Open JDK added to your source list. This can be done with following commands.

$ sudo add-apt-repository ppa:openjdk-r/ppa
$ sudo apt-get update

Step 2: Installing the JDK

To install the JDK we can use the standard apt-get way to install. At the time of the blog Java 11 is the current LTS and Java 12 was just made available. So I will be installing both the LTS and current versions in this examples.

Installing Java 11

To install Java 11, here is the command,

$ sudo apt-get install openjdk-11-jdk

Please answer questions as prompted.

Verify you have the correct JDK installed

Run the below command
$ java -version; javac -version
openjdk version "11.0.3" 2019-04-16
OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu218.04.1)
OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu218.04.1, mixed mode, sharing)
javac 11.0.3


Installing Java 12

To install Java 12, here is the command,

$ sudo apt-get install openjdk-12-jdk

Please answer questions as prompted.

Verify you have the correct JDK installed

Run the below command

$ java -version; javac -version                             
openjdk version "12.0.2" 2019-07-16
OpenJDK Runtime Environment (build 12.0.2+9-Ubuntu-118.04)
OpenJDK 64-Bit Server VM (build 12.0.2+9-Ubuntu-118.04, mixed mode)
javac 12.0.2


Good Work!. You have Java 11 and Java 12. Now it will be interesting to see how to switch the JDKs

Step 3: Switching between Java

Now that you have 2 versions of the Java installed, we will see how the alternatives get into action.

List the alternatives

To list the currently available alternatives we can use the below command,
$ sudo update-java-alternatives -l                          
java-1.11.0-openjdk-amd64      1111       /usr/lib/jvm/java-1.11.0-openjdk-amd64
java-1.12.0-openjdk-amd64      1211       /usr/lib/jvm/java-1.12.0-openjdk-amd64
java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64
java-1.8.0-openjdk-i386        1081       /usr/lib/jvm/java-1.8.0-openjdk-i386

Depending on what versions you have installed, you should see the list of versions installed. So from the output, the first column is the package name and that is what we are most interested for switching

Switching to the alternatives

If you have followed the instructions alongside, you should have Java 12 as you current JDK. This can be verified with this command.

$ java -version; javac -version
openjdk version "12.0.2" 2019-07-16
OpenJDK Runtime Environment (build 12.0.2+9-Ubuntu-118.04)
OpenJDK 64-Bit Server VM (build 12.0.2+9-Ubuntu-118.04, mixed mode)
javac 12.0.2

Form earlier, if you want to switch to Java 11 as default, please run the following command. Remember, based what is installed, you should locate the corresponding name.

$ sudo update-java-alternatives -s java-1.11.0-openjdk-amd64
update-alternatives: error: no alternatives for appletviewer
update-alternatives: error: no alternatives for extcheck
update-alternatives: error: no alternatives for idlj
update-alternatives: error: no alternatives for javah
update-alternatives: error: no alternatives for jhat
update-alternatives: error: no alternatives for jsadebugd
update-alternatives: error: no alternatives for mozilla-javaplugin.so
update-alternatives: error: no alternatives for native2ascii
update-alternatives: error: no alternatives for schemagen
update-alternatives: error: no alternatives for wsgen
update-alternatives: error: no alternatives for wsimport
update-alternatives: error: no alternatives for xjc

I guess we can ignore the error. If we now see the java and javac version it should have switched to Java 11.

$ java -version; javac -version
openjdk version "11.0.3" 2019-04-16
OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu218.04.1)
OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu218.04.1, mixed mode, sharing)
javac 11.0.3


Congragulations! You have now made it possible to switch the Java versions at will.

Step 4: Setting the correct JAVA_HOME

Since we are chasing an arbitrary Java, we will have to take a simple step towards getting the correct JAVA_HOME.

Here is what you can setup in your profile file like, .bashrc, .zshrc etc.

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

Here is what I saw after I ran the above command.

$ echo $JAVA_HOME                                                   
/usr/lib/jvm/java-11-openjdk-amd64/

Conclusion

By using the alternatives approach, we will be able to switch JDK fairly quickly.If you wish to contact me please visit https://contact.vpv.io/




Comments

Popular posts from this blog

Java SSL/TLS Testing Tool: Cipher Suite

PWA: Building and publishing a PWA in 15ish minutes using Angular 5 and Service Worker

Yarn and Angular-CLI