2013-07-08

Updating Environment Variables in Windows

This weekend I installed the Java Runtime Environment and the Java Development Kit onto my desktop and my laptop, both running identical Windows 7 environments. The installation onto the desktop went without a hitch. I ran both the JRE and JDK installers, got the JetBrains IntelliJ IDEA Java IDE up and running, and tried executing my first Java 'Hello World' program from the console. Shockingly, this worked on the very first try with no errors, and from the Git Bash, no less! This was such a pleasant surprise and unexpected result that it even prompted me to rename my dev blog to 'Unexpected Result'!

But for whatever reason, after going through all the same steps on my laptop, I couldn't seem to get Java to run from Git Bash. Upon checking my environment variable (typing env | grep PATH in Git Bash), I discovered that the JDK directory somehow didn't seem to have been added!

It turns out the solution was a simple matter of closing the Git Bash and reopening it! When a PATH variable change is registered, that change won't be visible to any running instances of Git Bash. It seems that Git Bash reads the PATH variable from Windows one time only, when the CLI is launched, and doesn't refresh it until the CLI is closed and launched again. This is true of other command line interfaces in Windows, as well (such as the Windows command prompt).

So if you've just added something new to a Windows environment variable, and it doesn't seem to be working from your CLI, make sure you have restarted your CLI after the environment variable was modified and it should be smooth sailing from there!

Thanks for reading!
-- Steven Kitzes