0

I used to use like this.

JAVA_HOME=C:\...
Path=...;%JAVA_HOME%\bin

And I want to use two separated variables for JAVA_HOME and tried this.

JAVA7_HOME=C:\...
JAVA8_HOME=C:\...
JAVA_HOME=%JAVA8_HOME%
Path=...;%JAVA_HOME%\bin

And it seems not work. The actual value of Path just contains %JAVA8_HOME%.

C:\Users\whoami\>echo %Path%
...;%JAVA8_HOME%\bin;...

C:\Users\whoami\>

How can I make this work?

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
  • Take a look here for example: http://stackoverflow.com/questions/10558316/example-of-delayed-expansion-in-batch-file – Arvo Apr 30 '15 at 10:32
  • 1
    I assume you're defining these in the control panel environment variable dialog. The assignment order of `REG_EXPAND_SZ` variables (i.e. the value has at least two `%` symbols) is undefined since the enumeration order of values in a registry key is undefined. Thus a `REG_EXPAND_SZ` variable such as `Path` should depend directly on only simple `REG_SZ` variables such as `JAVA7_HOME`. – Eryk Sun Apr 30 '15 at 17:38

1 Answers1

1

SET Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.

set "JAVA7_HOME=C:\..."
set "JAVA8_HOME=C:\..."
set "JAVA_HOME=%JAVA8_HOME%"
set "Path=%Path%;%JAVA_HOME%\bin"

Note " double quotes in all SET "variable=string": used to ensure there are no unwanted spaces in variable name or string value.

Any extra spaces around either the variable name or the string will not be ignored. SET is not forgiving of extra spaces like many other scripting languages.

Note PATH Display or set a search path for executable files. Hence, instead of

set "Path=%Path%;%JAVA_HOME%\bin"

you could use simply

Path=%Path%;%JAVA_HOME%\bin
JosefZ
  • 28,460
  • 5
  • 44
  • 83