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!

Wednesday, January 8, 2014

Setting Environment Variables on Unix and Windows

In this post I will try and explain the different ways on how to set environment variables on both Windows and Unix operating systems. I am also going to address the scope when setting an environemt variable.

What environment variables are set on my machine?

You can easily check this by typing "set" into a command line terminal on both windows or unix.
Unix and windows:
How do you retrieve the value of a particular environment variable?
echo $environment_variable
echo %environment_variable%

What are environment variables?

Environment variables are operating system level variables that can affect the running of a process. This meaning is slightly obscure but let me try to explain it the way I understand it. The way I see it, environment variables are just aliases for file or directory paths. All they are are just short names or references to a directory on your system. The reason for their existence as you may have realised by now is that they are dynamic and are uniformly used in programs (ie. no hard coding paths).

There is however one special environment variable. This is the "path" variable. The path variable contains multiple directory locations separated by either a ";" on windows or ":" in unix. Each directory location references a program's binary files or executable files. As an example, if you were to type in "java" on the command line interface of both windows or unix, the OS would search each of the directories in the path variable and identify the executable or binary with the name java. When it identifies the executable matching the name, it will execute the process and terminate. This is a very interesting and important statement.

  1. The path variable is evaluated from left to right scanning each directory for an executable matching the name.
  2. It executes the first occurrence of the executable and terminates.
  3. If no matches are made, a command not found message is returned.

How to set environment variables? 

Environment variables can be set either programmatically or manually with both being equal but they can be set for different levels of scope.

Global Scope

This is the highest level and is available to all users of the system. 
On unix this can be achieved by adding the environment variable to /etc/profile or /etc/profile.d file. On windows you can set this by going to the properties of "My Computer" and then selecting the environment variables tab. Within this tab, you should see the System variables and User Variables. Set a System Variable.
You will only be able to set a variable on this level if you are an administrator on the system.  

Once an environment variable is set, you would need to restart any open command terminals for the change to take effect. If you were to restart your machine the variable would still be there. It is permanent.

User Scope

Environment variables set at this level is only available to the user that is logged in.
On unix this can be achieved by adding the environment variable to /Users/<username>/.bash_profile file. Once an environment variable is set, you would need to log in again for the change to take effect or you can execute the .bash_profile script which will set the environment variable for the open session.

On windows you can set this by going to the properties of "My Computer" and then selecting the environment variables tab. Within this tab, you should see the System variables and User Variables. Set a User variable.
Once an environment variable is set, you would need to restart any open command terminals for the change to take effect. If you were to restart your machine the variable would still be there. It is permanent for the user.

Session Scope

Environment variables set at this level only last for the duration that the command line terminal is open. It is lost as soon as the terminal is closed.
On unix this can be achieved by typing the following in a command line terminal: 
export VARIABLE=value  # for Bourne, Bash and related shells
setenv VARIABLE value  # for csh and related shells
On windows this can be achieved by typing the following in a command line terminal:

Script Scope

Environment variables set at this level only last for the duration that the script runs. It is lost as soon as the script terminates, unless the script is executed from command line terminal. In this case the session scope rules apply.
Same as in the session scope but is embedded in a shell or batch script.

Setting the "path" environment variable

All of the above applies to normal environment variables. Remember we said that the "path" variable is special and contains multiple directories. You can edit the path environment variable in the same way as mentioned for normal variables as above for the most part. 

Here are other variations which might be useful when setting the "path" environment variable.
When setting on windows use the ";" symbol as a separator:
SET path=C:/interesting/informing/techies;C:/important/informing/techies
When setting on unix use the ":" symbol as a separator:
export path=/interesting/informing/techies:/important/informing/techies
If you wanted to append or prepend a new directory location to the existing "path" environment variable:      
When setting on windows use the %VAR% notation:
SET path=%path%:C:/interesting/informing/techies;C:/important/informing/techies
When setting on unix use the $VAR notation:
SET path=$path:/interesting/informing/techies;/important/informing/techies

Remember prepending or appending to the path variable makes a huge difference!!!

Who uses and maintains environment variables?

Applications and programs use environment variables. They also maintain the environment variables by doing the necessary creations, deletions and updates during installation, updating and uninstalling processes. 

Therefore, messing around with environment variables without a clear and proper understanding can cause your system not to work as desired. My suggestion is that if you do want to test something out, try it out on the session scope. In this way your system is not permanently affected. 

With this said, you can also use environment variables in your applications. They are great at abstracting paths and are also a solution to hard coding. 


I hope that this gives you a clear understanding of environment variables and their scope. As always, I would appreciate any feedback and comments are always welcome.

If you have issues in your environment I am more than happy to lend a hand!