Friday, October 21, 2016

How To Update Oracle's Java On Ubuntu Linux

It's a given fact that all software developers/manufactures updates their products on a regular basis. Sometimes it's hard for us mere mortals to keep track of these updates, especially if the developers/manufactures do not supply the proper tools for detecting, downloading and installing these updates. Unfortunately, Oracle is a culprit here, at least for their Java products on Linux. We, as mere mortals, have to check their download web site for any updates on a regular basis. Perhaps, their brother gods can persuade them to give better support for Linux users. Well, enough of the tub-thumping. I will try my best to fill this gap and outline the procedure for upgrading their Java on Ubuntu Linux to the latest version.

I will assume that you have installed a 64-bit version of the latest Ubuntu. If you have a 32-bit version of Ubuntu, make sure that you replace 'x64' with 'x32' throughout this article.

Follow the following steps in order to update the Java installation:
  1. Point your web browser to the Oracle download page and download the latest version of the 64-bit JDK (not Server JRE or the JRE). It is also recommended that you download the API documents, demos and samples for the JDK, although this is not required for Android software development.

  2. Once the packages have been downloaded, execute the following commands in a terminal (making sure that you replace the 'X' in the commands below with the correct number as given on Oracle's web site):

      cd Downloads
      tar xzvf jdk-XuXXX-linux-x64.tar.gz
      tar xzvf jdk-XuXXX-linux-x64-demos.tar.gz
      unzip jdk-XuXXX-docs-all.zip -d jdkX.X.X_XXX

    The last two commands in the above is only applicable if you have downloaded the demos and the documentation. The second command above will create a 'jdkX.X.X_XXX' directory in the Downloads folder while the last two commands above (if applicable) will create the 'sample', 'demo' and 'docs' directories under the 'jdkX.X.X_XXX' directory in the Downloads folder.

  3. If you are updating an existing Oracle's Java installation, then the '/usr/lib/jvm/' directory already exists. Now execute the following commands in the same terminal:

      sudo mv jdkX.X.X_XXX/ /usr/lib/jvm/
      cd ~

    This will move the whole 'jdkX.X.X_XXX' directory to the root of all the Java-related files resides. The last command will place you back into your home directory.

  4. Notice that the owner and group of the 'jdkX.X.X_XXX' directory is still in your name and group. You can keep it as it is if you are the only user of the PC or you can set this to root by executing the following commands in the same terminal:

      sudo chown -R root:root /usr/lib/jvm/jdkX.X.X_XXX

    Again make sure that you replace the 'X' in the commands above with the correct number as given on Oracle's web site.

    The contents of the '/usr/lib/jvm' directory after this step may contain two (or more) entries – one is the newly updated version and the other is the old version of Java. Do not delete the old version for now.

  5. Now execute the following commands in the same terminal:

      sudo update-alternatives --remove-all java

    This will effectively remove the default Java from the Ubuntu system. In effect, this will 'unlink' the default Java from the old verstion. To set the new version of Java as the default Java, execute the following command in the same terminal:

      sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdkX.X.X_XXX/bin/java" 1500 --slave "/usr/share/man/man1/java.1" "java.1" "/usr/lib/jvm/jdkX.X.X_XXX/man/man1/java.1"

    Again make sure that you replace the 'X' in the commands above with the correct number as given on Oracle's web site.

    These will install all the necessary links to the new version of Oracle's Java, including a slave link for the 'man' pages for Java. After the command has been executed, the default Java is automatically set to Oracle's Java.

  6. To verify that all have been set correctly, you can use the following commands:

      update-alternatives --display java

    If everything have been set correctly, the output to the query above looks like this:

      java - auto mode
        link best version is /usr/lib/jvm/jdkX.X.X_XXX/bin/java
        link currently points to /usr/lib/jvm/jdkX.X.X_XXX/bin/java
        link java is /usr/bin/java
        slave java.1 is /usr/share/man/man1/java.1
      /usr/lib/jvm/jdkX.X.X_XXX/bin/java - priority 1500
        slave java.1: /usr/lib/jvm/jdkX.X.X_XXX/man/man1/java.1

  7. A quick check on the current version of the Oracle's Java can be performed by executing the following command in a terminal:

      java -version

    If Oracle's Java has been set up properly, the output for the command is:

      java version "X.X.X_XXX"
      Java(TM) SE Runtime Environment (build X.X.X_XXX-bXX)
      Java HotSpot(TM) 64-Bit Server VM (build XX.XX-bXX, mixed mode)

  8. We will now set the environment variable correctly for the whole Ubuntu system. Execute the following command in the same terminal:

      sudo nano /etc/profile

    This will execute the nano text editor and open the '/etc/profile' file for editing. Now edit the following line at the end of the file, while making sure that you replace the 'X' in the commands below with the correct number as given on Oracle's web site.

      export STUDIO_JDK=/usr/lib/jvm/jdkX.X.X_XXX
      export JDK_HOME=/usr/lib/jvm/jdkX.X.X_XXX
      export JAVA_HOME=/usr/lib/jvm/jdkX.X.X_XXX

    Save the file and close it. Note that, upon system startup, these will set the STUDIO_JDK, JDK_HOME and JAVA_HOME environment variables for the whole system. These environment variables points to the newly updated Oracle's Java JDK root. Note that these environment variables will be used for Android Studio, and other programs which uses Java.

  9. Now log out and then log in again to the system in order to make sure that the environment variable are set. Check that you have correctly installed Oracle Java by executing the commands (one by one) in a newly opened terminal:

      echo $STUDIO_JDK
      echo $JDK_HOME
      echo $JAVA_HOME
      java -version
      javac -version

    If you see the string 'jdkX.X.X_XXX' in the output to the above commands (where the 'X' in the in the output is the correct number as given on Oracle's web site), then you have successfully updated Oracle's Java.

  10. If you are using Android Studio and you have set the JDK pointer to point to the old JDK, then you need to reset the pointer to point to the newly updated one. To do this, launch Android Studio and at the welcome screen, click on Configure > Project Defaults > Project Structure. At the screen that appear, make sure that the JDK location field is pointing to the updated Java at /usr/lib/jvm/jdkX.X.X_XXX (where the 'X' is any number). Note that if you are using the embedded JDK (which is marked as recommended), then you can skip this this step entirely.

  11. You can now delete the old version of Oracle's Java from the '/usr/lib/jvm' directory if you wish to free some disk space.

That's it! You now have a fully updated PC with the latest version of Oracle's Java.

No comments:

Post a Comment