44

I am trying to set up some path using environment variable. I added an environment variable "MAVEN_HOME" with the value "C:\maven". Then in the path I added "%MAVEN_HOME%\bin;...rest". When I type "echo $MAVEN_HOME%" I get the correct "C:\maven" printed on the screen. But when I type "mvn" which is a batch file in the "bin" directory, it can't find it.

So, I manually added the entire path in PATH. "C:\maven\bin;...rest" and it was able to find "mvn" and execute it.

Could someone help me what I did wrong?

2
  • What does echo %PATH% output?
    – calebds
    Dec 31, 2011 at 2:05
  • 5
    I had the same problem and found it to be a path length issue. I created new environment variables, P86 and P64, to replace all occurrences of "C:\Program Files (x86)" and "C:\Program Files" references in the existing PATH variable. The new path (System + User) is currently 2754 characters after expansion of the new variables. I read in similar threads that the theoretical limit is 32KB but apparently that is not the practical limit.
    – hknust
    Aug 27, 2014 at 16:07

14 Answers 14

76

Check if there is a space character between the previous path and the next:

Incorrect: c:\path1; c:\Maven\bin\; c:\path2\

Correct: c:\path1;c:\Maven\bin\;c:\path2\

1
  • 4
    I can see that I have spaces (from cmd and python) but I dont know how to get rid of them. I've been banging my head against the wall for like 3 hours now!
    – Alex
    Aug 5, 2016 at 15:49
59

I had exactly the same problem, to solve it, you can do one of two things:

  • Put all variables in System Variables instead of User and add the ones you want to PATH

Or

  • Put all variables in User Variables, and create or edit the PATH variables in User Variable, not In System. The Path variables in System don't expand the User Variables.

If the above are all correct, but the problem is still present, you need to check the system Registry, in HKEY_CURRENT_USER\Environment, to make sure the "PATH" key type is REG_EXPAND_SZ (not REG_SZ).

8
  • 2
    Hi @pcmind, mine is set to REG_SZ, what should i do now? Apr 27, 2015 at 14:55
  • make a copy of the value of "PATH" key (with type REG_SZ), delete "PATH" key, create a new key with type REG_EXPAND_SZ and paste in the old value. restart computer.
    – Zhe Hu
    Jul 21, 2015 at 14:37
  • This could be an option . However, the suggestion below makes more sense. If there is no space kind of problem, even if you define in both user and system level, it should work.
    – Saman
    Dec 11, 2015 at 1:23
  • Windows 10 has the same issue and fix stated by pcmind.
    – gt124
    Aug 17, 2016 at 23:14
  • This answer saved me tons of frustration and explained some serious discrepancies on my path variable: The Path variables in System don't expand the User Variables. Nov 8, 2016 at 16:43
53

My issue turned out to be embarrassingly simple:

Restart command prompt and the new variables should update

2
  • 17
    Do not ignore this answer; 9 times out of 10 this is the problem.
    – aguertin
    Jul 6, 2016 at 21:53
  • This happens because each process gets a set of environment variable and when you quit and start cmd ,the process will get new path variables
    – yeah_well
    May 21, 2020 at 9:12
16

Things like having %PATH% or spaces between items in your path will break it. Be warned.

Yes, windows paths that include spaces will cause errors. For example an application added this to the front of the system %PATH% variable definition:

C:\Program Files (x86)\WebEx\Productivity Tools;C:\Sybase\IQ-16_0\Bin64;

which caused all of the paths in %PATH% to not be set in the cmd window.

My solution is to demarcate the extended path variable in double quotes where needed:

"C:\Program Files (x86)\WebEx\Productivity Tools";C:\Sybase\IQ-16_0\Bin64;

The spaces are therefore ignored and the full path variable is parsed properly.

2
  • It CAN cause errors for a small minority of users. I am investigating something similar, but your conclusion is incorrect here. Most users have paths appended or prepended with spaces w/o issue, the semicolon is a fine delimiter that means no quotes required, but we have found that for some users this can be true. Why is that some small minority of users are affected in this way, but not others? Unknown as of this time. That is why that app added itself as such, it was not some simple omission.
    – dyasta
    Aug 6, 2017 at 11:13
  • sometimes it doesn't help. In my case - C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Scripts. It gives error anyway
    – Andrey
    Mar 17, 2021 at 11:13
5

%M2% and %JAVA_HOME% need to be added to a PATH variable in the USER variables, not the SYSTEM variables.

1
  • 1
    Not true. I have them in SYSTEM variables. For me, it was the spaces in the paths, like C:\Program Files, who caused the OP problem. Nov 8, 2017 at 11:04
5

If there is any error at all in the PATH windows will silently disregard it. Things like having %PATH% or spaces between items in your path will break it. Be warned

2
  • This worked for me! Had no idea about this whatsoever.
    – user1300214
    Jan 17, 2014 at 17:20
  • For example I just noticed that my path included the literal text %NVM_HOME% even though this is a defined variable.. this was fixed after I noticed there was a %CHOCOLATEY_HOME% immediately prior, left there after I uninstalled chocolatey, so there was no definition for that variable. I removed the chocolatey variable from the path, and then the NVM_HOME variable got correctly replaced with its value. It's surprising a basic issue like this remains in windows. Makes me wonder what else is broken...
    – Razzle
    Mar 29, 2020 at 13:06
5

Also worth making sure you're using the command prompt as an administrator - the system lock on my work machine meant that the standard cmd just reported mvn could not be found when typing mvn --version

To use click 'start > all programs > accessories', right-click on 'command prompt' and select 'run as administrator'.

4

To address this problem, I have used setx command which try to set user level variables.

I used below...

setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0_92"

setx PATH %JAVA_HOME%\bin

NOTE: Windows try to append provided variable value to existing variable value. So no need to give extra %PATH%... something like %JAVA_HOME%\bin;%PATH%

1
  • 2
    There is alternative way by going to Control Panel --> All Control Panel Items --> User Accounts and then click on 'Change my environment variables' in the left navigation.. but somehow it is not working in Windows 7 and 10 . Till Microsoft address that we can use above way of setx. May 29, 2016 at 8:19
3

If the PATH value would be too long after your user's PATH variable has been concatenated onto the environment PATH variable, Windows will silently fail to concatenate the user PATH variable.

This can easily happen after new software is installed and adds something to PATH, thereby breaking existing installed software. Windows fail!

The best fix is to edit one of the PATH variables in the Control Panel and remove entries you don't need. Then open a new CMD window and see if all entries are shown in "echo %PATH%".

2

I had this problem in Windows 10 and it seemed to be solved after I closed "explorer.exe" in the Task Manager.

2

In my Windows 7.

// not working for me
D:\php\php-7.2.6-nts\php.exe

// works fine
D:\php\php-7.2.6-nts
1
  • This was my issue. Thanks!
    – Akaisteph7
    May 31, 2019 at 13:24
1

I had the same problem, I fixed it by removing PATHEXT from user variable. It must only exist in System variable with .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

Also remove the variable from user to system and only include that path on user variable

0

Copy the value of path to notepad and check if this corresponds with the echo %path% in terminal window and make changes if needed. Then delete the old path value and paste the notepad value back in. I assume some invisible character entered there by some installation corrupted the path value.

0

Make sure both your System and User paths are set correctly.

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