Problems with setting environment variables (especially PATH) and using cd with them in cmd in windows 10

command lineenvironment-variablespath

I am following a tutorial to setup Apache Storm. (That's just for context — it doesn't really matter for my error.)

So it tells me to set some environment variables, specifically %JAVA_HOME% and %STORM_HOME% as C:\Java\jdk1.8.0_60\ and C:\storm-0.9.1-incubating-SNAPSHOT-12182013\, respectively. I've never done this sort of thing before, so I looked up how to do this and I ran:

C:\>setx %JAVA_HOME% C:\JAVA\jdk1.8.0_60\

and it gave: SUCCESS: Specified value was saved.

Then I did:

C:\>setx %STORM_HOME% C:\storm-0.9.1-incubating-SNAPSHOT-12182013\

and it gave the same thing: SUCCESS: Specified value was saved.

Then the tutorial says to add %STORM_HOME%\bin;%JAVA_HOME%\bin;C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\; to PATH. I entered C:\>PATH to see what it currently is and it gave:

PATH=C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\

So I thought I could add to it by doing

C:\>setx PATH C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;%STORM_HOME%\bin;%JAVA_HOME%\bin;C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\; 

In other words, I just tried calling setx on PATH and I set it to what it currently is and with the stuff the tutorial says appended to the end. It gave SUCCESS: Specified value was saved. However, when I enter:

C:\>PATH

it returns what it was originally (the stuff I tried appending is not there). I tried another method. I ran

C:\>PATH=%PATH%;%STORM_HOME%\bin;%JAVA_HOME%\bin;C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\;

Then when I entered C:\>PATH it returned the full PATH with the stuff I wanted to append appended.

The problem I now face is using these variables. When I enter

C:\>cd %STORM_HOME%

it says The system cannot find the path specified. I tried restarting my computer but PATH got reset and I still got the same error.

Best Answer

@DavidPostill is correct, but doesn't actually resolve your problem. The actual answer is much simpler: Command Prompt, like any program, receives its environment variable at startup. When you set a persistent environment variable (either per-user or system-wide), you are changing the registry values that determine the environment variables for future processes. You aren't actually changing any environment variables in the current process at all!

From the SETX help documentation (setx /?):

 2) On a local system, variables created or modified by this tool
    will be available in future command windows but not in the
    current CMD.exe command window.

There are two ways to address this. The simple one is, as the documentation indicates, start a new instance of CMD. That's easy enough, but then you won't have your command history, etc. The more complicated one is to set the local environment variables (use the set command). The best way to do this is actually to use set first, and then invoke setx using the current environment variables. Here's an example, from what you are doing above:

REM Set the local version of JAVA_HOME
set JAVA_HOME=C:\JAVA\jdk1.8.0_60\
REM Set the local version of STORM_HOME
set STORM_HOME=C:\storm-0.9.1-incubating-SNAPSHOT-12182013\
REM Append the new values to PATH
set PATH=%PATH%;%STORM_HOME%\bin;%JAVA_HOME%\bin;C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\;
REM Do whatever checks you want to do to confirm that those are set correctly
cd %STORM_HOME%
REM Now, save the current (local) values of the environment variables
REM to persistent storage (registry)
setx JAVA_HOME %JAVA_HOME%
setx STORM_HOME %STORM_HOME%
setx PATH %PATH%

Note the use of % characters. You use them when you went to expand an environment variable, not when you want to refer to one. The setx lines above are processed as "Set the user-wide environment variable JAVA_HOME to this process' current local value of JAVA_HOME", which is equivalent to saying "Set the persistent environment variable JAVA_HOME to C:\JAVA\jdk1.8.0_60\" because that's what %JAVA_HOME% expands to in the current process.

Related Question