Jin Kwon
Jin Kwon

Reputation: 21976

How can I reuse environment variables in Windows?

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?

Upvotes: 0

Views: 922

Answers (1)

JosefZ
JosefZ

Reputation: 30103

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

Upvotes: 1

Related Questions