24

May be question is asked before but i found very strange kind of error. When I run mvn --version command from bin its gives the version and the other information. However when I run outside bin it gives exception. Both the result mention below

C:\Program Files\apache-maven-3.0.5-bin\apache-maven-3.0.5\bin>mvn --version
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 19:21:
28+0530)
Maven home: C:\Program Files\apache-maven-3.0.5-bin\apache-maven-3.0.5
Java version: 1.7.0_25, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_25\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

C:\Program Files\apache-maven-3.0.5-bin\apache-maven-3.0.5>mvn --version
'mvn' is not recognized as an internal or external command,
operable program or batch file.

Also i set JAVA_HOME in user variable and M2_HOME, M2 in system environment variable. The output for these variables and %path% is:

C:\Users\SAHILBHALLA>echo %JAVA_HOME%
C:\Program Files\Java\jdk1.7.0_25

C:\Users\SAHILBHALLA>echo %M2%
C:\Program Files\apache-maven-3.0.5-bin\apache-maven-3.0.5\bin

C:\Users\SAHILBHALLA>echo %M2_HOME%
C:\Program Files\apache-maven-3.0.5-bin\apache-maven-3.0.5

C:\Users\SAHILBHALLA>echo %path% 
C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPow erShell\v1.0\;C:\Program Files\Java\jdk1.7.0_25\bin;.; 

Tell me where i am wrong. What should i do recover from it?

Cadoiz
  • 1,446
  • 21
  • 31
rahul
  • 1,062
  • 5
  • 15
  • 31
  • 2
    Did you place those environment variables to PATH? – Rohit Jain Jul 02 '13 at 18:12
  • 1
    is the location of mvn.exe in your %PATH%? e.g. your second mvn invocation is actually happening one directory UP from where you did the first one, so I'm guessing it's not in the path. try `bin\mvn` as the command instead. – Marc B Jul 02 '13 at 18:14
  • Marc B if I do `bin\mvn` then i will not able to access from any other location( from my workspace) – rahul Jul 02 '13 at 18:36
  • See the solution here: http://stackoverflow.com/a/36601957/2114308 – Phuong Apr 13 '16 at 14:44
  • You can consider [this related Q/A](https://stackoverflow.com/q/33589226/4575793). Be aware that (at least for recent Windows) there is a difference between user (above) and system environment variables such as `path`. [Screenshot here](https://i.stack.imgur.com/zHtTQ.png) – Cadoiz Sep 13 '21 at 13:15

18 Answers18

28

This problem arise because there is no any environmental variable corresponding to installed maven in your OS. For fixing this problem, I always use Intellij's bundled Maven and do not install separate version of Maven again, for finding bundled Maven's path go to intellij and hit Ctrl+Alt+S -> Build, Execution, Deployment -> Build tool -> Maven -> Maven home directory you can find the intellij's bundled maven path there as below image demonstrates.

enter image description here

Then go to System environment variables and set these variables:

Variable name: MAVEN_HOME

Variable value: C:/Program Files/JetBrains/IntelliJ IDEA 2019.3.1/plugins/maven/lib/maven3

enter image description here

After defining system variable MAVEN_HOME find variable path and add this line to the list

%MAVEN_HOME%\bin

enter image description here

Work is done, open command prompt and test it by writing mvn -v. 99 percent of the time it work, if you're among 1 percent, you have to restart your computer.

If you want to use mvn command from intellij's internal terminal you have to restart intellij after setting environment variables, then you shouldn't have any problem running maven command from terminal.

Davoud
  • 2,576
  • 1
  • 32
  • 53
24

My problem solved, path didn't resolve %M2%. When i added location of maven-bin in the path instead of %M2% after that commands works.

I would like to thanks to all those who try to solve the problem

rahul
  • 1,062
  • 5
  • 15
  • 31
  • the same happened to me and I also had to restart my PC because somehow when I used `echo %PATH%` I kept seeing the old path I don't know why. – Reem Jan 01 '14 at 22:58
  • When i added the path this error gone. But when i restart the system it again shows this error. The path is there in environment variables. But i want to delete and set the path again to make it work. Why? – Mohammed Javad Nov 02 '18 at 05:34
14

maven should be on the system's PATH if you wish to execute it from any place. add %M2_HOME%\bin to the PATH

Cadoiz
  • 1,446
  • 21
  • 31
radai
  • 23,949
  • 10
  • 71
  • 115
  • as i mention earlier M2_HoME and M2 set in system environment variable – rahul Jul 02 '13 at 18:38
  • right... which is why @radai is having you add %M2_HOME%/bin to %PATH%. trust me, this will work. – thekbb Jul 02 '13 at 21:06
  • @thekbb - whenever you type in the name of an executable or shell (bat) file, the operating system looks for it in the current directory youre in. if it doesnt find it there it goes over all of the directories that are mentioned in %PATH%. if you want mvn to be accessible then %M2_HOME%\bin _HAS_ to be on the path. the fact that %M2_HOME% exists is not enough (although that too is needed) – radai Jul 03 '13 at 03:14
  • @rahul but %M2% is not where mvn is located, %M2%\bin is what should be on the path – radai Jul 03 '13 at 04:21
  • M2 already contain bin as echo gives `C:\Program Files\apache-maven-3.0.5-bin\apache-maven-3.0.5\bin` – rahul Jul 03 '13 at 05:04
10

First of all make sure you java is working or not run this command in cmd

 C:\>java -version

if it's working it will show this output:-

C:\>java -version
java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) Client VM (build 25.74-b02, mixed mode)

step 1. First set your java_home[C:\Program Files\Java\jdk1.8.0_74] path in user variable.

step 2. Then set MAVEN_HOME[C:\Program Files\maven\apache-maven-3.3.9] path in system variable and make sure your maven folder should be present in C folder only.

step 3. Then set M2 path in system variable and give maven bin location there i.e.[C:\Program Files\maven\apache-maven-3.3.9\bin].

Step 4. Then set new system variable i.e. variable name = MAVEN_OPTS in and variable value =-Xms256m -Xmx512m

Step 5. Then edit path/system path variable be care full don't remove anything from there simply add java_home path i.e=;C:\Program Files\Java\jdk1.8.0_74 and M2 variable=;%M2% in the end.

Step 6. To make sure maven is now working or not run this command in cmd

> C:\>mvn --version

if it's working it will show this result :-

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-
7+05:30)
Maven home: C:\Program Files\maven\apache-maven-3.3.9\bin\..
Java version: 1.8.0_74, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_74\jre
Default locale: en_IN, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "dos"
seema choudhary
  • 103
  • 1
  • 6
8

In Windows 10, I had to run the windows command prompt (cmd) as administrator. Doing that solved this problem for me.

Contributor123
  • 101
  • 1
  • 3
5

In windows 7, I Got it resolved after adding the environment variables in system level. If you do not have enough permission try to set the %JAVA_HOME% and the %M2_HOME% in System variables instead of User Variables.

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
Jineesh
  • 11,187
  • 5
  • 24
  • 25
4

Place the full path to mvn in your PATH environment variable.

rgettman
  • 176,041
  • 30
  • 275
  • 357
4

To solve this problem please follow the steps below:

  1. Download the maven zip file from http://maven.apache.org/download.cgi
  2. Extract the maven zip file
  3. Open the environment variable and in user variable section click on new button and make a variable called MAVEN_HOME and assign it the value of bin path of extracted maven zip
  4. Now in System Variable click on Path and click on Edit button --> Now Click on New button and paste the bin path of maven zip
  5. Now click on OK button
  6. Open CMD and type mvn -version
  7. Installed Maven version will be displayed and your setup is completed
Cray
  • 2,774
  • 7
  • 22
  • 32
shashikant
  • 59
  • 3
3

After setting the path with the Maven bin location close the prompt window and restart it. Use the echo %path% command to make sure the path is set with Maven variables then run the command mvn -version. Somehow if the path is set when the prompt window is running it does not pick the new variables.

SDJMcHattie
  • 1,690
  • 1
  • 15
  • 21
  • 1
    Thanks for contributing an answer to StackOverflow. To make your answer easier to read, you can put commands you want formatted in monospace font between back ticks such as this `. I've updates your answer for you to reflect this. – SDJMcHattie Mar 03 '14 at 13:49
3

The following helped me in Win10.

  • Add %M3_HOME%\bin; as value for Path variable under User Variables.
  • Add full path to maven binary folder as Variable Value for M3_HOME variable under System Variables.
  • Add %M3_HOME%\bin; as value for Path variable under System Variables.
  • Click OK wherever applicable.
  • Close the existing command prompt.
  • Open new command prompt and navigate to Maven binary folder.
  • Type mvn -version

It will work.

Skandix
  • 1,916
  • 6
  • 27
  • 36
Sagar
  • 35
  • 5
1

Here is the best Maven-Environment Setup tutorial for Windows, Unix and Mac Operating systems.

But in the last you have to set value of PATH variable as ";%M2_HOME%\bin" instead of "%M2%", because PATH variable is not able to reduce the value using "%M2%"

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
vkrishna17
  • 906
  • 10
  • 17
1

got it solved by first creating new "Path" variable under User variables (note that after fresh windows install Path variable is not created as User variable, only as system) after that, I appended %M2% (pointing to maven dir/bin) to (freshly created) user Path variable. after that restarted cmd window and it worked like a charm.

Padvinder
  • 971
  • 1
  • 6
  • 9
1

Go to the shell (cmd for windows) and set the path variable manually from there. It works often from there. Read more at http://www.howtogeek.com/118594/how-to-edit-your-system-path-for-easy-command-line-access/

Kay
  • 799
  • 1
  • 11
  • 29
1

Go to Environment Variable and paste the following:

Under System Variable: Step 1: New --> New User Variable 1. Variable name: MAVEN_HOME 2. Variable_value : D:\apache-maven-3.5.2

Step 2: 1. Go to the path --> and paste this - %MAVEN_HOME%\bin

shashi
  • 400
  • 3
  • 8
1

Try these steps...

cmd : mvn -version

go apache-maven folder > bin

copy the path and go System properties > environment variables > system variables

path > edit > [paste the file path]

next restart your machine.

https://youtu.be/RQ_Z859Hd7Q

Arosha
  • 511
  • 1
  • 7
  • 19
0

I prefer adding path to ~/.bashrc.

vim ~/.bashrc, then add these lines:

export M2_HOME=/usr/local/apache-maven-your_maven_path&version
export M2=$M2_HOME/bin
Freya Ren
  • 2,086
  • 6
  • 29
  • 39
0

I did all of this in Windows 10 and still had a problem. In the end it turned out that the path to the Maven home folder was not exactly what was expected in many of these answers as it turned out to be /apache-maven-3.6.3-bin/apache-maven-3.6.3. Once I corrected this for both the system variables and the PATH variable, it worked. In short, if you have set the environment variables up as directed and it still won't work, I would double check to make sure the variables really point to the exact path to the Maven home folder and the bin folder on your machine.

0

i do like this , i use with intellij

C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.1.2\plugins\maven\lib\maven3

and you also need to add this in the Path variable

click Path -> Edit -> New -> and paste it so it references to just created


%M2_HOME%\bin
Long tran
  • 71
  • 1
  • 2