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.)

link|improve this question
For all you guys voting to close (3 at the moment) ... if there's a dup somewhere, a comment pointing me to it sure would be nice. If it's not a dupe ... then telling me what you think is wrong with this question would also be nice. – skiphoppy Aug 21 '09 at 18:40
1  
Perhaps because it is more a system question than a programming one, although it has a direct impact on programming, that's why I don't vote to close it... :) – PhiLho Aug 21 '09 at 18:49
1  
Attenion close-nazis: I'd like to promote the view that if a question was appropriate on Stack Overflow before superuser.com and serverfault.com arrived, then it is still appropriate today. This is a programming question. – skiphoppy Aug 21 '09 at 19:12
feedback

migrated from stackoverflow.com Aug 22 '09 at 17:20

This question came from our site for professional and enthusiast programmers.

6 Answers

there are two levels of environment variables, global and user. If he has %Java_home% set as a user environment variable but is instead changing the global one, he won't see any difference.

link|improve this answer
feedback

i asked this on the Microsoft forums in March 2009, and never got it resolved:

How to use %ProgramFiles% in the Path environment variable?:


I'm trying to add a folder to the system's Path environment variable.

i want to add %ProgramFiles%\SysInternals

to the existing path variable:

C:\PROGRA~1\Borland\Delphi5\Projects\Bpl;C:\PROGRA~1\Borland\Delphi5\Bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Microsoft SQL Server\80\Tools\BINN;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\

So i go into place where you edit it:

alt text

And i add my variable to the path:

%ProgramFiles%\SysInternals;C:\PROGRA~1\Borland\Delphi5\Projects\Bpl; (snip)

Then opening a new command prompt window the environment variable is not replaced with it's actual value:

Path=%ProgramFiles%\SysInternals;C:\PROGRA~1\Borland\Delphi5\Projects\Bpl(snip)>

Which you can see in the following screenshot:

alt text


But to answer your question: i don't know. Seems like it cannot be done.

link|improve this answer
feedback

It might be related to the "delayed environment variable expansion" feature (or lack thereof), or perhaps you can take advantage of this feature to always have a correct solution.

from a cmd prompt

set /?

and read the section describing "delayed environment variable expansion", which includes a small example to test

set VAR=before
if "%VAR%" == "before" (
    set VAR=after
    if "%VAR%" == "after" @echo If you see this, it worked
)

If you don't get the echo line, then that might explain it...

If, however, you start your cmd.exe with /V option, then you can use "!" instead of "%", which changes the behaivior

set VAR=before
if "%VAR%" == "before" (
    set VAR=after
    if "!VAR!" == "after" @echo If you see this, it worked
)

For me (running on XP), the 1st script did not work, but the second version did (with cmd.exe /V)

link|improve this answer
feedback

Perhaps you are doing it wrong?

I tried with Windows XP Pro SP3 (32bit). I do have a path with several occurrences of %JAVA_HOME% (and %JAVAFX_HOME%, etc.). I go to command line, type PATH, I see the variables expanded. Good.

I change the value of JAVA_HOME. Back to the same command line window, PATH again, same value... as expected (by experience!).

I open a new command line window, type PATH, gotcha, I see the new value.

Not sure what is the exact mechanism there, but it seems that any running program, including cmd.exe, capture the values of environment variables at starting time, and doesn't look back... (although I believe a good behaved program can listen for env changes, not too sure though).

It might be seen as a feature or a bug or annoyance, but that's the way it works. Hey, at least, unlike Win9X times, we don't have to reboot the computer! And unlike NT times (IIRC), you don't have to log out and back.

Why the inconsistency? The ways of Microsoft are inscrutable... :-P

link|improve this answer
This isn't it. After the change we're testing in a fresh command window. We're aware of the fact that changing the system values doesn't change the values for running processes. – skiphoppy Aug 21 '09 at 19:11
OK, hence the 'perhaps'... :-) And my explanation doesn't cover the inconsistency, but might be useful to some newbie... :-P I mostly wanted to point out that variable expansion works everywhere in the path... for some systems! (all those I used... always 32bit). – PhiLho Aug 22 '09 at 8:38
feedback

I've had the same problem, and I know how to fix it, its lame.

Just edit your PATH again, but make no change, and re-save PATH. For some reason this causes all nested environment variable references to get re-evaluated.

If it doesn't work do it a few more times, somehow it just works itself out.

link|improve this answer
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.