9

I have two user defined variables:

ANT_HOME set to %USERPROFILE%\Developer\Tools\Apache\Apache Ant\apache-ant-1.8.2 JAVA_HOME set to C:\Program Files\Java\jdk1.6.0_23

And I want append those paths to the System variable so I can access the executables in their bin folders. I tried

PATH set to [other paths];%ANT_HOME%\bin;%JAVA_HOME%\bin

However, that doesn't work. When I open a command-prompt and echo %PATH% the user variables are not expanded. So the path just shows the literal, ;%ANT_HOME%\bin;%JAVA_HOME%\bin

I seem to recall this working fine on my old Windows XP systems. How do I set this up on Windows 7?

1
  • I just found a Microsoft Support article from way back in 2007 describing a bug in Shell32.dll. I added a PATH variable to my user variables list with PATH set to %PATH%;%ANT_HOME%\bin;%JAVA_HOME%\bin and worked-around the issue. I don't like setting it for just my user, but somehow my system is presenting the symptom of this old bug and I see no other better answer yet. The article is at: support.microsoft.com/kb/329308 Jan 21, 2011 at 19:05

2 Answers 2

5

UPDATE NO.2 - Now to the actual question: Why do nested, user-created variables fail to expand?

There's some general problems concerning variable expansion in Windows. I've already run into the same problem and found no clear, reproducible circumstances - the recursion level at which expansion fails is not consistent, special characters don't seem to play a role, etc.

The only viable workaround I found is adding variables recursion level by recursion level. That means: Try deleting all variables you want to nest into each other (including calls from PATH to your user-defined variables), and then start up from scratch. Define your basic variables (etc. ANT-HOME), commit, check if it's expanded, if it is, go on with the next level commit, check... you get the idea.

UPDATED ANSWER - Defining permanent environment variables using the CLI and GUI - Scroll down for the original answer

GUI method:

On Windows 7, just type "system" in the META-Prompt and you'll see an entry "Edit the System Environment Variables". From there, click "Environment variables". There, you can either edit the system variable PATH (bottom list) or add/edit a new PATH variable to the user environment variables.

Command line method:

To change environment variables permanently, you have to use the SETX command in the Windows command line. Unlike in other versions of Windows, it comes built-in with Windows 7. Its syntax differs a lot from SET, but it's also powerful. You'll have to be a bit careful though, it's easy to make a mess of your variables with SETX.

  • By default, you change user variables. You can have a PATH user environment variable that happily coexists with the system PATH variable. If you don't have it defined yet, do so by typing: SETX PATH yourpath

  • You can also add a value to the system variable PATH. To do this, you first need to bring up a command line with admin privileges. To do this, hit the Meta(Windows) key, type cmd and hit CTRL+ SHIFT+ENTER and confirm the UAC dialog.

To add new values to path, you can now enter

setx path "%path%;yournewpath" /m

It's important to follow that syntax! If you don't include %path% first, all existing values of path will be lost and replaced with only you new path. The /m switch at the end sets the variable in the system environment.

Please note that you'll have to bring up a new command line to make use of your new variable.

There's also a full reference for SETX at TechNet.

OLD ANSWER The command SET updates the variables only for the duration of the current command line session.

The correct syntax for adding a value to a variable is 'set [variable]=%[variable]%;[new value]`

Note that left of the equal sign, you have to omit the percent signs!

Source: TechNet Command-line reference for Windows Server

3
  • 1
    Thank you for a very detailed response. I personally have always preferred the GUI method because I think it is easier to communicate to my team. I've highlighted the problem I'm having in my original post for clarity. The user variables are not expanding in Windows 7 as they did in Windows XP. Does this no longer work or should I be investigating security or other system administrator settings that may be blocking the usual behavior? Jan 21, 2011 at 18:45
  • That's a very nice way of saying "You're completely beside the point" ;) Jan 21, 2011 at 21:05
  • ... and now my 2 cents on the actual question. See newest answer edit. Jan 21, 2011 at 21:15
1

A Microsoft Support article from way back in 2007 — number 329308 — describes a bug in Shell32.dll, which is the cause of this problem. The solution is to add a PATH variable to the user variables list that is set to

%ANT_HOME%\bin;%JAVA_HOME%\bin

This worked around the issue. I don't like setting it for just my user, but somehow my system is presenting symptoms of this old bug and I see no other better answer yet.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .