Tuesday, January 21, 2014

Back to Java Basics - Installing Java

I can't remember learning how to install java at varsity. Maybe, the instructions were on the first tutorial and I never had to bother with it again or it might have just been installed for us. The point being; when I started at my first job and had to install java on both my Windows machine and my allocated workspace on a Unix, I just could not get it right.

Was I crazy?

This might have been the case but then again whenever I feel an inference in the direction that I am crazy, I often find solace in believing that everyone else in this world is crazy and I being the only sane one. Moving on ... I did google the usual "How to install java on windows" and followed the instructions meticulously but still it did not work. You can imagine how I felt when I had to ask one of the seniors to help me install java. It was not straight forward for him as well. He spent about 30 minutes to an hour. Messing around with environment variables through command line and all kinds of weird nonsense. Eventually he got it working and I was amazed. My first thoughts were that if getting java installed was so difficult, how would I cope as a java developer? It turned out that the windows machine I inherited belonged to a techie who installed every possible software he could get his hands on. You might be asking: but this has nothing to do with installing java???

Oh, you are so wrong. I eventually got to understand the sorcery behind installing java. Up to this day I still find senior java engineers struggling with this concept but in a different problem space. As a practising consultant we often have to work with systems that are running in production with multiple versions of java installed but running some undesired version. I have seen even the most senior engineers spending hours trying to sort this mess out.

By contrast to the title, I am not going to show you how install java. I am going to clear up all the mystery that surrounds:
  • Installing java step by step and it just not working for you
  • Having installed a specific version of java but when running "java -version" it says that it is running some other obscure version

Understand Environment Variables

It is of utmost importance that you understand how environment variables work. I have written an article and would suggest that you read this article very carefully and become comfortable with the concepts before proceeding:

Setting Environment Variables on Unix and Windows

If you reached this point, you are really comfortable with environment variable ;-)
Cool, let us continue...

Installing Java

For me there are just 3 important parts to installing java:

  1. The jdk package (jdk folder)
  2. Setting the JAVA_HOME environment variable
  3. Updating the path variable to reference the jdk binary or executables

Point number 2 is not even required, you will understand later.

Once you have the jdk package (folder containing bin, include, jre, lib & Other files) you are 90% there. If you type java through a command line at this point, unfortunately java will not work. The only thing required here is to get the operating system to reference the java binary or executables. If you have read my other post, you should have realised that I am talking about setting the path variable. All we need to do is add the path / location of the bin directory to the path environment variable.

Now, if you try and run java from a command line it should work. If you have followed my instructions correctly and it is still not working then; open up a new command line terminal (fully explained in my other post) or this is were things get really interesting.

Why is the JAVA_HOME environment variable not important? 

The JAVA_HOME environment variable would contain a reference to the base directory of jdk. When setting the path, it is advised that you only add JAVA_HOME/bin so that the operating system can find the binaries executable files. There is no functional difference by specifying the full directory path to the bin directory. It is merely a standard and preferred convention.

Why the convention?

The convention is there so that if you reference an alternate version of java, you would only need to update the JAVA_HOME environment variable. The path variable is quite long containing multiple references to different executable files. It can become "difficult" working with this long string and a typo can affect some other program.
So in a nutshell, the JAVA_HOME environment variable is a convenience variable for the purposes of this article. For your broader knowledge, the JAVA_HOME environment variable might be referenced by programs that require java. Certain web servers or application servers might need this variable be set in order for it to function. It uses this variable to reference libraries or files for it to function correctly.

Installing java today(The Interesting part I spoke about earlier)

I have noticed that when you download a java jdk today, it is packaged with an installation procedure. This procedure completes the process mentioned above on your behalf but even if you use the installer, you might still not get java to work or an undesired version might be running.

Other programs mess with the path variable 

There are so many programs, applications, servers and processes that require java. When installing them, they either reference the java installation already installed on your machine (good program) or contain it's own minimised / customised jdk or jre. If they are installed and append to your path variable, you are done! What happens is that every time you run a java comand, the system evaluates the path variable from left to right and looks in each directory for an executable called java. It will find the first occurrence, which in this scenario is a minimised / customised program specific one execute and terminate. I have personally been bitten by this when installing Oracle server and even some profiling tools.
How do we fix this?
I specifically highlighted, bolded, increased font size and underlined the word "add" earlier in this post. When most people hear add they think that it is append(put at the back). You can even prepend(put in the front) the path variable. This is a very cool trick. Now you have a way of guaranteeing that your desired java executable is executed first. Prepending the path variable saves you from figuring out which reference is problematic. You can figure this out by removing the references one at a time up until you get it working.


The path variable can be a source of major headaches but if you understand both environment variables really well and what is required to install java you should be fine. You would also be comfortable hosting multiple versions of java and be able to reference a specific version if and when required by merely changing your JAVA_HOME environment variable.

As Always, I would love to hear your comments or suggestions on how I can improve this post. I am also more than willing to lend a hand if you are having issues in your environment!

No comments:

Post a Comment