"Inheriting" path variables in Windows

1

In Windows 7 I have added two user environment variables:

M2_HOME   %PROGRAMFILES%\Apache\apache-maven-3.0.2
M2        %M2_HOME%\bin

In my head, both of those should work, however, only the first one does for some reason. What I mean is that I in a command prompt can type

cd %M2_HOME%

Which works fine, but

cd %M2%

Which does not. What's going on here?

Svish

Posted 2011-01-18T07:45:35.327

Reputation: 27 731

Reboot.​​​​​​​​ – Synetech – 2011-06-30T00:46:54.997

Answers

2

There seems to be a bug with nested environment variables in Windows 7 (maybe other versions too, I'm not sure) where if the name of the top level variable comes before the name of the nested variable alphabetically, it won't be expanded.

In your case, the top level variable M2 comes before the nested variable M2_HOME when sorted alphabetically. If you change M2 to something that comes after M2_HOME (eg, M3), you will see that it works as intended.

Herohtar

Posted 2011-01-18T07:45:35.327

Reputation: 527

Now that's annoying... Guess that's also the reason why it doesn't work to add %M2% to the path as well. Any way to specify the order of these environment variables? – Svish – 2011-01-18T08:44:10.027

The only way to get around the problem is to do what I said and rename M2 to something like M3 or even something like M2_PATH would work -- as long as it's something that comes after M2_HOME when compared alphabetically. Alternatively, you could edit M2_HOME and manually enter the program files directory instead of using %PROGRAMFILES%, as the bug only seems to occur when you have nesting of more than two levels. – Herohtar – 2011-01-18T18:37:59.750