Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wish to define two environment variables as follows:


Obviously the following works:


But, the following doesn't:

dir %JAVA_DIR%

What should I do in the case I do wish to define JAVA_DIR in terms of PROGRAM_HOME?

EDIT: removed \ (it had been a typo)


share|improve this question
What exactly "doesn't work"? Do you get an error message? – fretje Jan 4 '10 at 14:55
"The system cannot find the file specified." This is because, if I set the variable from the GUI dialog the %PROGRAM_HOME% variable does not get expanded. I use Rapid Environment Editor, by the way. – Yaneeve Jan 4 '10 at 15:30
Then your problem is apparently that Rapid Environment Editor doesn't support expanding other environment variables. – fretje Jan 4 '10 at 18:03
up vote 1 down vote accepted

Apart from the syntax error found by fretje, if you wish to make these variables permanent, then you need to change the Windows environment variables.

This is done in Control Panel / System / Advanced tab / Environment variables... button.

There is also a nice little product called Rapid Environment Editor (donationware) that provides a clearer user interface.


I think I understand now what you're trying to do, but that can only be done by editing the registry. The following Microsoft article explains the process to follow:
How to propagate environment variables to the system.

In a nutshell, for user environment variables you need to go to registry key HKEY_CURRENT_USER\Environment, where all these variables are stored.

There you need to add the two variables as of type REG_EXPAND_SZ (named "expanded string value" in regedit). This is the only way to add this type, since Windows by default will only add REG_SZ types which are simply unexpandable.

I tried it just now and it works perfectly well.

share|improve this answer
I do use that Editor, but it can only 'expand' env vars embedded within other env vars to a depth of 1, hope I made myself clear – Yaneeve Jan 4 '10 at 14:51
Got it! See my edit. – harrymc Jan 4 '10 at 16:49

I think it's because you repeat the '\'.

When I do the following:

>set %program_home%=c:\
>set %java_dir%=%program_home%java
>dir %java_dir%

everything works fine.

Of course an alternative is:

>set %program_home%=c:
>set %java_dir%=%program_home%\java
>dir %java_dir%

You can verify the values of the different environment variables by simply issuing an set command without parameters. Then you'll see that your %java_dir% variable contains c:\\java.

share|improve this answer
ok, that works, but I wish to set the vars, once and for all, not via the cmd. What would you suggest? – Yaneeve Jan 4 '10 at 13:40
Yaneeve, I think you may be mis-reading his answer. Remove the \ in "JAVA_DIR=%PROGRAM_HOME%\Java" in your environment variable and it should work. Note that the syntax fretje uses here with set can be used identically with system environment variables. – RJFalconer Jan 4 '10 at 14:07

Yes this a really odd behavior that I have never found documented but here is what works.

Using your example above put quotes around the variable in the last line:

dir "%JAVA_DIR%"

If someone knows the why of this or a link that explains it this would be great. The behavior is consistent though when referring to a variable that has been defined in part with another variable.

share|improve this answer
For some reason this works only if I use the 'set' command to define the variable within the same process and not otherwise :| – Yaneeve Jan 4 '10 at 11:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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