[java] Unable to install Maven on Windows: "JAVA_HOME is set to an invalid directory"

I followed the Maven tutorial to the letter but I still can't get Maven installed on Windows.

When I run the following in command prompt:

E:\Documents and Settings\zach>mvn --version

I get:

'mvn' is not recognized as an internal or external command, operable program or batch file.

I navigated to the maven install folder and ran mvn --version and got:

E:\java resources\apache-maven-2.2.0\bin>mvn --version
ERROR: JAVA_HOME is set to an invalid directory.
JAVA_HOME = "E:\Sun\SDK\jdk\bin"
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation

but when I run java -version I get:

java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode)

So I do have Java installed. Anyone know what the problem is?

This question is related to java maven-2

The answer is


I had the same issue and none of the above answers fixed it for me because my env variables were all set. I had just reinstalled my Java.

What worked was to

  1. go to the C:\path\to\apache-maven-3.0.4\bin and open the mvn.bat file.
  2. Find the line that looks like this @SET JAVA_HOME=C:\progra~1\java\jdk1.7.0_03
  3. Correct it to the right path

I don't know if this is Windows specific, but it might help someone!


ERROR: JAVA_HOME is set to an invalid directory. JAVA_HOME = "E:\Sun\SDK\jdk\bin" Please set the JAVA_HOME variable in your environment to match the location of your Java installation

JAVA_HOME should be set to E:\Sun\SDK\jdk. PATH should be set to include %JAVA_HOME%\bin.


I was facing the same issue and just updated the JAVA_HOME worked for me.

previously it was like this: C:\Program Files\Java\jdk1.6.0_45\bin Just removed the \bin and it worked for me.


I ran into this issue with a Grails install.

The problem was my JAVA_HOME was c:\sun\jdk\ and my PATH has %JAVA_HOME%bin

I changed it to: JAVA_HOME= "c:\sun\jdk" and PATH="%JAVA_HOME%\bin"

It worked after that.


JAVA_HOME should be like this C:\PROGRA~1\Java\jdk

Hope this will work!


The JDK has switched locations of java.exe between 1.6 and 1.7!!!

In my case I found that the JAVA_HOME for the JDK had to add the \jre on the end. The mvn bat file is looking for java.exe and it looks for it in JAVA_HOME\bin. Its not there for JDK 1.7; it is in JAVA_HOME\jre\bin. In JDK 1.6 such it IS in JAVA_HOME\bin.

Hope this helps somebody.


After lot of failed tried attempts ,I found the solution

It was the ";" at end of JAVA_HOME which I always put at end of each new variable I set. So get rid of the ;.

JAVA_HOME set it in User Variable also (without the ";" ofcourse)


you should set JAVA_HOME or MAVEN_HOME without bin directory for example: - JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_45 - MAVEN_HOME=C:\Program Files (x86)\apache-maven-3.1.1 now path=.....;%MAVEN_HOME%\bin;%JAVA_HOME%\bin it's work correctly


My situation was a bit different.

  • JAVA_HOME was set properly to point to 1.7
  • Other Maven projects were working/building fine with 1.7 features.
  • PATH was set properly.
  • Everything was up-to-date.

Still my simple new Maven project was not working. What I noticed was the difference in the logs when I ran mvn clean install. For my older Maven projects, it showed

[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ oldProject---

But for my new project it showed:

[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ newProject ---

So, I looked at the POM.xml and noticed this thing in the old project's POM:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Basically, this plugin tells which compiler version to use for compilation. Just added it to the new project's POM.xml and things worked.

Hope it is useful to someone.


Running eclipse and also running Maven will require you to store two path variables, one in your jdk1.7_x_x_x location and also in your jdk1.7_x_x_\bin. If you are using Windows, when you are in your environment variables, do the following:

1) create a USER variable called JAVA_HOME. Point this to the location of your JAVA file. For example: "C:\Program Files\Java\jdk1.7.0_51" (remove the quotes)

2) under the PATH, append %JAVA_HOME% to the PATH. This will add the file location from step 1 to your PATH. This is good for MAVEN

3) if you are using eclipse you need to have the path point to "C:\Program Files\Java\jdk1.7.0_51\bin". Now append %JAVA_HOME%\bin to the end of your path.

4) your path should look something like this: C:\Program Files (x86)\Google\google_appengine\;C:\Users\username\AppData\Roaming\npm;%M2%;%JAVA_HOME%;%JAVA_HOME%\bin

Notes: the items that are enclosed in %'s like %M2% are assigned variables. It looks redundant but necessary. You can confirm that everything works by typing in:

java -version
javac -version
mvn -version

Each of those three statements typed in comman prompt should not return errors.


This seems to be old post but still I wanted to share how this issue got fixed for me.

For users, who do not have admin access and when they open a command prompt, it runs under the user privilege. It means, you may have path like C:\Users\

so when trying C:\Users\XYZ>mvn --version , it actually search the JAVA_HOME path from user variables not system variables in Environment Variables.

So, In order to fix this, we need to create a environment variable for JAVA_HOME in user variables.

Hope, this helps someone.


Sometimes in Windows whitespaces in paths are not recognized correctly

If you have a path problem and path seems like

c:\Program Files\....

try changing it in an old DOS format like

"C:\Progra~1\...

You can use dir /x to check correct syntax (third column)

C:\>dir /x ... 11.01.2008 15:47 <DIR> DOCUME~1 Documents and Settings 01.12.2006 09:10 <DIR> MYPROJ~1 My Projects 21.01.2011 14:08 <DIR> PROGRA~1 Program Files ...

In my pc JAVA_HOME is (and it works)

"C:\Progra~1\Java\jdk1.8.0_121"

Tested in Windows 10


Due to security restrictions at my current place of work I was unable to set enviroment variables on my Windows based PC.

My workaround was to copy the mvn.bat file from %M2% into C:\WINNT and add the following to the top of the batch file:

@REM Needed as unable to set env variables on my desktop PC.

set MAVEN_OPTS=-Xms256m -Xmx1024m
set M2_HOME=C:\apache-maven-3.0.4
set M2=%M2_HOME%\bin
set JAVA_HOME=C:\Program Files\Java\jdk1.5.0_15
set PATH=%JAVA_HOME%\bin;%M2%;%PATH%

Not the nicest solution but it works. If anybody has any other way or work-around where the standard env vars are not able to be set into the system I'd welcome their response.


I am using Windows 7, the problem I had was simple:

I had this for my JAVA_HOME environment variable value:

"C:\Program Files\Java\jdk1.7.0_51;"

when it wants:

"C:\Program Files\Java\jdk1.7.0_51"

the semi-colon strikes again!

:)

This is how I solved this problem, so this should be one possible solution.


using windows 10

I was facing issue .. then I removed JAVA_HOME variable completly and just added %JAVA_HOME%\bin in PATH then it worked!!! for mee