Windows – Why can’t Windows handle an environment variable in Path

environment-variableswindows

My colleague and I have identical Dell workstations with Windows XP Professional x64 edition installed.

My Path environment variable starts with:

%JAVA_HOME%\bin;...

My colleague's Path variable includes the same directory, specified using the same environment variable, but it is not the first item in his Path.

If I access system properties -> environment variables and change the value of my JAVA_HOME variable, the version of java found from the command-line changes as I expect. This is starting a brand new console window, to be sure to pick up the changes.

But on my colleague's machine, it does not. He continues to find his previous version of Java until he brings up his Path variable and saves it (even if he makes no changes to it). (Again, this is when starting a brand new console window.)

I have been observing this inconsistency on Windows for about 6 months now and very curious about it. We have way too many versions of Windows in our office, so rarely have I had a chance to see this happening on two machines running the exact same OS version, until now.

What is causing this? Why does his machine not re-evaluate Path, using the new JAVA_HOME, when mine does?

(Is it because it's not the first thing in the Path? If so, how could that be, and why? I'd do more tests to check, but I think he's now fed up with it and would like to get back to work.)

Best Answer

Your path is the concatenation of the system path followed by the user path. Additionally, system environment variables may not contain references to user environment variables, and any such references will not be expanded. To get the desired result, insert the reference to %JAVA_HOME% in the user environment variable PATH, or create such a variable if it doesn't already exist.

Perhaps a simplified example will make this clearer. Suppose the SYSTEM environment is

ProgramFiles = C:\Program Files
SystemRoot = C:\WINDOWS
PATH = %SystemRoot%\SYSTEM32

and the User JSmith's environment is

JAVA_HOME = %ProgramFiles%\Java\bin
USERPROFILE = C:\USERS\JSmith
PATH = %JAVA_HOME%\bin;%USERPROFILE%\bin

then the resulting path would be

C:\WINDOWS\SYSTEM32;C:\Program Files\Java\bin;C:\Users\JSmith\bin

as desired.

Related Question