Thursday, January 29, 2015

How To Remove OpenJDK 7 JRE From Linux Mint 17.1

Recently, I became curious as to whether it was possible to delete the default OpenJDK 7 JRE and switch to totally using Oracle's Java - including using Oracle's Java plugin for Firefox. This blog describes my experience with this effort, and the short answer is "Yes - it is possible". Here is how:

STEP 1: First you have to download the latest version of Oracle's Java SE (aka JDK) package and install it. To do this, first point your Firefox web browser to the Oracle SE download page here and download the latest version of the 64-bit JDK (not Server JRE or the JRE) - currently at version 8u31 (or 1.8.0_31). It is also recommended that you download the API docs and demos (+samples) for the JDK - although this is not required for Java program development. Once the packages have been downloaded, close the Firefox web browser and execute the following commands:

  cd Downloads
  tar xzvf jdk-8u31-linux-x64.tar.gz
  tar xzvf jdk-8u31-linux-x64-demos.tar.gz
  unzip jdk-8u31-docs-all.zip -d jdk1.8.0_31
  sudo mv jdk1.8.0_31/ /usr/lib/jvm/
  sudo chown -R root:root /usr/lib/jvm/jdk1.8.0_31
  cd ~


The above steps will uncompress the files downloaded to a 'jdk1.8.0_31' directory in the 'Downloads' folder. The 'jdk1.8.0_31' directory will then be moved to /usr/lib/jvm, where all the current Java files reside. The owner:group will then be set to 'root'.


WARNING: If you do not move the 'jdk1.8.0_31' directory to /usr/lib/jvm directory now, then the /usr/lib/jvm directory will be removed when you remove OpenJDK 7 later.


STEP 2: Then we have to remove the IcedTea plugin from Firefox. If you launch Firefox now, and type "about:plugins" in the location (URL) bar, it will show that 'IcedTea-Web Plugin (using IcedTea-Web 1.5 (1.5-1ubuntu1))' is one of the plugins installed. It will not be possible to remove this plugin using Firefox's -> Tools -> Add-ons as this plugin was installed by OpenJDK itself. So we have to turn to the Synaptic Packet Manager to do the job. Make sure that Firefox is closed, then launch Synaptic and enter 'icedtea' into the search box. Then mark the following items for complete removal:

  icedtea-7-plugin
  icedtea-plugin
  icedtea-7-jre-jamvm
  icedtea-netx
  icedtea-netx-common


Click the 'Apply' button to remove the marked files. You can then check whether the Firefox Java plugin has been removed by launching Firefox and typing "about:plugins" in the location (URL) bar. The resulting display should indicate that there is no Java plugin present now. Then close Firefox again. A quick look into the /etc/alternatives directory will also show that the 'itweb-settings', 'jexec' and 'mozilla-plugin.so' files has also been removed.

STEP 3: Now we will replace Firefox Java plugin by Oracle's version. Let me stress that this step is optional - if you wish to test Java applets in Firefox, go ahead and install this plugin - else just ignore this step.

First check whether there is a sub-directory called 'plugins' under the '.mozilla' directory (a hidden directory - hence the period before the name) in your home directory. The 'ls -la .mozilla' command is used to perform this check.

If this directory is not present then create the directory by executing the 'mkdir .mozilla/plugins' command within your home directory. Once this directory is present, its time to create a link to Oracle's Java plugin. The name of this plugin is 'libnpjp2.so' and is found in the /usr/lib/jvm/jdk1.8.0_31/jre/lib/amd64 directory. Execute the following commands:

  cd .mozilla/plugins
  ln -s /usr/lib/jvm/jdk1.8.0_31/jre/lib/amd64/libnpjp2.so .
  cd ~


Do not forget to include the period (.) in the second last command above. It denotes that the link will be created in the current directory (.mozilla/plugins). Once this link is created, launch Firefox again, type 'about:plugins' in the location box and the resulting display will show that Oracle's Java plugin is present. Also, if you click on Tools -> Add-ons, followed by selecting the 'Plugins' tab, you will see that the Java plugin is present.

STEP 4: Launch Synaptic again and this time, type 'openjdk' into the search box. Now mark the following files for complete removal:

  default-jre
  default-jre-headless
  openjdk-7-jre
  openjdk-7-jre-headless
  openjdk-7-jre-lib


Click the 'Apply' button to remove the marked files. If you look into the /usr/lib/jvm directory, you will find that there is now just one directory there -  the one containing Oracle's Java. The files in the /etc/alternatives directory will no longer contain links to Java.


NOTE: If you have set up links previously, you can now remove those links using the 'update-alternatives --remove-all itemname' (where itemname is the name of the Java link you wish to remove). Just make sure that you only remove the Java related links - removing other non-Java links may harm the whole system. It is safe to remove these links as there is one Java installed - Oracle's SE 8. The list of the files are:

  java
  javaws
  keytool
  orbd
  pack200
  policytool
  rmid
  rmiregistry
  servertool
  tnameserv
  unpack200



STEP 5: Now we have to set the environment variables and paths correctly for the whole system. If you have already set these variables previously, then you can safely skip this step. If not, execute the following command:

  sudo nano /etc/profile

This will execute the nano text editor and open the /etc/profile file for editing. Now add the following lines at the end of the file.

  export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_31
  export PATH=$PATH:$JAVA_HOME/bin


Save the file and close it. Note that, upon system startup, these will set the JAVA_HOME and PATH environment variables for the whole system. The JAVA_HOME environment variable points to the JDK root while the PATH environment variable will add the /bin directory of the JDK to whatever path that has already been specified. This will enable the 'javac' command to be executed from any directory. Now log out and then log in again to the system in order to make sure that both the environment variables are set.

STEP 6: Check that you have correctly installed Oracle Java by executing the commands:

  echo $JAVA_HOME
  echo $PATH
  java -version
  javac -version


If you see the string 'jdk1.8.0_31' in the output to the above commands, then you have successfully removed OpenJDK 7 JRE and replaced it with Oracle SE 8.

Now whenever Oracle issue an updated version, all you have to do in order to use the new version of Java SE is to follow STEP 1, STEP 3 and STEP 5 above. Just make sure that you change the version number to the new version in the commands. No more messing around with 'update-alternative' commands - and that alone make this whole exercise worthwhile. Happy computing!

2 comments:

  1. Great post. To continue using Netbeans IDE after this change of JDK from OpenJDK to Oracle JDK just edit your start menu by right-clicking or otherwise and locate the following.

    /bin/sh "/home/datta/netbeans-8.0.2/bin/netbeans"

    Since this command won't work unchanged anymore, just add the jdkhome switch so that the command becomes the following

    /bin/sh "/home/datta/netbeans-8.0.2/bin/netbeans" --jdkhome $JAVA_HOME

    ReplyDelete
  2. Thanx a hole lot!,

    The effort & skills behind a fine work will always be appreciated.
    Keep up the good work!

    Mr. Z

    ReplyDelete