How to use Oracle JDK 7 on CircleCI Ubuntu 14.04 machines

Recently I have worked on a 'boring' Java project which uses OpenJDK 7. I want to continuous integrate that project using CircleCI and I had tried to build it on a Linux Ubuntu 14.04 (Trusty), which is the newest version that CircleCI offers. While doing so, I encountered a problem, which, after narrowed down the scope, I found that not occur when I used OracleJDK or build on Linux Ubuntu 12.04 (Precise).

I don't want to downgrade the OS version and I still have not figured out what goes wrong with OpenJDK, so I have temporarily chosen OracleJDK on Ubuntu 14.04. I also stay with version 7 because I don't want to upgrade the JDK version to version 8.

CircleCI Ubuntu 14.04 does not have OracleJDK 7 pre-installed

As mentioned in CircleCI build images page, CircleCI's Ubuntu 14.04 machines has JDK 6, 7, 8 pre-installed, both OracleJDK and OpenJDK; and oraclejdk7 is the default version unless you describe a version you want in the machine section, like:

  version: oraclejdk7

And I would like to know if the default version is truly JDK 7. I didn't explicitly select and I write a command to log out the Java version of that machine after configuring:

- sudo java -version

Here is what I got:

I decided to list all the sub-directory of /usr/lib/jvm/, the locations where Java libraries stored.

- sudo ls /usr/lib/jvm/

And there I saw:
OpenJDK7, OpenJDK8 and OracleJDK8 are installed but no sign of OracleJDK7!!!

Manually install OracleJDK 7 on CircleCI's machine

You could hardly install OracleJDK from Ubuntu terminal while you could easily install an OpenJDK one.
One of the way is that using a PPA which is showed here. It was the first approach I came up with, but it asked for user input too much, and was inappropriate on an automatic environment like CircleCI so I gave up trying it.
Another way is described here, includes 3 main step: download the JDK compressed file, decompress it, and use update-alternatives commands to install. This way requires less user input and it appeared to be faster when I tested on my PC.

The problem of that approach was that you needed Cookie to download a link from Oracle Download pages. And thanks to that link, I got a completed command to download a version of JDK 7 via wget. Here is the command:

- sudo wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" ############## all in ONE line

 But at the first time, that command couldn't run on CircleCI because the YAML parser recognized a colon and a space as a key-value separator and it throw-ed an error. I have to work around by making my circle.yml look like this instead:

- | sudo wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" ############## break into TWO lines

Below is the total commands required for downloading, decompressing and installing like the post shared:

post: - | sudo wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" - sudo tar -xvf jdk-7u79-linux-x64.tar.gz - sudo mv ./jdk1.7.0_79 ./jdk1.7.0 - sudo mv ./jdk1.7.0 /usr/lib/jvm/ - sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0/bin/java" 1000 - sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0/bin/javac" 1000 - sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0/bin/javaws" 1000 - sudo chmod a+x /usr/bin/java - sudo chmod a+x /usr/bin/javac - sudo chmod a+x /usr/bin/javaws - sudo chown -R root:root /usr/lib/jvm/jdk1.7.0

The last problem was that to switch between JDK version via the command
         update-alternatives --config java
you had to send user input as the number, the order of your option. And again, thanks to Ubuntu users community, I found a way:
         update-alternatives --config java <<< "<the_order_of_OracleJDK7_on_the_list>"

And I hard-coded the number because I knew quite sure it was number 3.

After doing all of that, I printed another log of the Java version and I finally got this:

OracleJDK7, which is introduced that pre-installed but actually not installed yet on CircleCI's Ubuntu 14.04, has been installed by me!!!


Popular posts from this blog

How to wrap a C# library for using in Java, a folk tale about an idiot coding at midnight.