简体   繁体   English

无法在Windows上安装Maven:“JAVA_HOME设置为无效目录”

[英]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. 我遵循了Maven教程 ,但仍然无法在Windows上安装Maven。

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: 我导航到maven安装文件夹并运行mvn --version并得到:

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我得到:

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. 所以我确实安装了Java。 Anyone know what the problem is? 谁知道问题是什么?

The problems are to do with your paths. 问题与你的道路有关。

  1. Make sure that the directory "E:\\java resources\\apache-maven-2.2.0\\bin" is on your command search path. 确保目录“E:\\ java resources \\ apache-maven-2.2.0 \\ bin”位于命令搜索路径中。
  2. Make sure that the JAVA_HOME variable refers to the home directory for your Java installation. 确保JAVA_HOME变量引用Java安装的主目录 If you are executing Java from "E:\\Sun\\SDK\\jdk\\bin", then the JAVA_HOME variable needs to point to "E:\\Sun\\SDK\\jdk". 如果从“E:\\ Sun \\ SDK \\ jdk \\ bin”执行Java,则JAVA_HOME变量需要指向“E:\\ Sun \\ SDK \\ jdk”。

    NB: JAVA_HOME should NOT end with "\\bin" 1 . 注意:JAVA_HOME不应以“\\ bin”结尾1

  3. Make sure that you haven't put a semicolon in the JAVA_HOME variable 2 . 确保没有在JAVA_HOME变量2中放置分号。

    NB: JAVA_HOME should be a single directory name, not "PATH-like" list of directory names separated by semicolons. 注意:JAVA_HOME应该是单个目录名,而不是以分号分隔的“PATH-like”目录名列表。

Also note that you could run into problems if you have ignored this advice in the Maven on Windows instructions about spaces in key pathnames. 另请注意,如果您在关于路径名中的空格的Maven on Windows指令中忽略了此建议,则可能会遇到问题。

"Maven, like many cross-platform tools, can encounter problems when there are space characters in important pathnames ." “像许多跨平台工具一样,Maven 在重要路径名中有空格字符时会遇到问题。”

"You need to install the Java SDK (eg from Oracle's download site), and you should install it to a pathname without spaces , such as c:\\j2se1.6."' “您需要安装Java SDK(例如,从Oracle的下载站点),您应该将其安装到没有空格的路径名 ,例如c:\\ j2se1.6。”

"You need to unpack the Maven distribution. Don't unpack it in the middle of your source code; pick some location ( with no spaces in the path! ) and unpack it there." “你需要解压缩Maven发行版。不要在源代码中间解压缩它;选择一些位置( 路径中没有空格! )并在那里解压缩。”

The simple remedy for this would be to reinstall Java or Maven in a different location so that there isn't a space in the path 对此的简单补救措施是在不同的位置重新安装Java或Maven,以便路径中没有空间


1 - .... unless you have made an insane choice for the name for your installation location. 1 - ....除非您为安装位置的名称做出了疯狂的选择。

2 - Apparently a common "voodoo" solution to Windows path problems is to whack a semicolon on the end. 2 - 显然,Windows路径问题的常见“伏都教”解决方案是最终敲击分号。 It is not recommended in general, absolutely does not work here. 一般不推荐,绝对不能在这里工作。

ERROR: JAVA_HOME is set to an invalid directory. 错误:JAVA_HOME设置为无效目录。 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 =“E:\\ Sun \\ SDK \\ jdk \\ bin”请在您的环境中设置JAVA_HOME变量以匹配Java安装的位置

JAVA_HOME should be set to E:\\Sun\\SDK\\jdk . JAVA_HOME应设置为E:\\Sun\\SDK\\jdk PATH should be set to include %JAVA_HOME%\\bin . 应将PATH设置为包含%JAVA_HOME%\\bin

I ran into this issue with a Grails install. 我用Grails安装遇到了这个问题。

The problem was my JAVA_HOME was c:\\sun\\jdk\\ and my PATH has %JAVA_HOME%bin 问题是我的JAVA_HOMEc:\\sun\\jdk\\而我的PATH%JAVA_HOME%bin

I changed it to: JAVA_HOME= "c:\\sun\\jdk" and PATH="%JAVA_HOME%\\bin" 我将其更改为: JAVA_HOME= "c:\\sun\\jdk" and PATH="%JAVA_HOME%\\bin"

It worked after that. 之后它起作用了。

I was facing the same issue and just updated the JAVA_HOME worked for me. 我遇到了同样的问题,刚刚更新了JAVA_HOME为我工作。

previously it was like this: C:\\Program Files\\Java\\jdk1.6.0_45\\bin Just removed the \\bin and it worked for me. 以前是这样的:C:\\ Program Files \\ Java \\ jdk1.6.0_45 \\ bin刚刚删除了\\ bin,它对我有用。

Due to security restrictions at my current place of work I was unable to set enviroment variables on my Windows based PC. 由于我目前工作地点的安全限制,我无法在基于Windows的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: 我的解决方法是将mvn.bat文件从%M2%复制到C:\\ WINNT并将以下内容添加到批处理文件的顶部:

@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 had the same issue and none of the above answers fixed it for me because my env variables were all set. 我有同样的问题,上面的答案都没有为我修复,因为我的env变量都设置好了。 I had just reinstalled my Java. 我刚刚重新安装了我的Java。

What worked was to 有用的是

  1. go to the C:\\path\\to\\apache-maven-3.0.4\\bin and open the mvn.bat file. 转到C:\\path\\to\\apache-maven-3.0.4\\bin并打开mvn.bat文件。
  2. Find the line that looks like this @SET JAVA_HOME=C:\\progra~1\\java\\jdk1.7.0_03 找到看起来像这样的行@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! 我不知道这是否是Windows特定的,但它可能对某人有帮助!

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. 在JAVA_HOME的末尾,我总是把它放在我设置的每个新变量的末尾。 So get rid of the ;. 所以摆脱;

JAVA_HOME set it in User Variable also (without the ";" ofcourse) JAVA_HOME也将它设置在用户变量中(没有“;”)

你应该设置没有bin目录的JAVA_HOME或MAVEN_HOME,例如: - 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它正常工作

Sometimes in Windows whitespaces in paths are not recognized correctly 有时在Windows中,路径中的空格无法正确识别

If you have a path problem and path seems like 如果你有一个路径问题和路径似乎

c:\\Program Files\\....

try changing it in an old DOS format like 尝试以旧的DOS格式更改它

"C:\\Progra~1\\...

You can use dir /x to check correct syntax (third column) 您可以使用dir /x来检查正确的语法(第三列)

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) 在我的电脑JAVA_HOME是(它的工作原理)

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

Tested in Windows 10 在Windows 10中测试过

JAVA_HOME should be like this C:\\PROGRA~1\\Java\\jdk JAVA_HOME应该像这样C:\\ PROGRA~1 \\ Java \\ jdk

Hope this will work! 希望这会奏效!

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\\ 这意味着,你可能有像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. 所以在尝试C:\\ Users \\ XYZ> mvn --version时,它实际上是从用户变量搜索JAVA_HOME路径而不是环境变量中的系统变量。

So, In order to fix this, we need to create a environment variable for JAVA_HOME in user variables. 因此,为了解决这个问题,我们需要在用户变量中为JAVA_HOME创建一个环境变量。

Hope, this helps 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. 运行eclipse并运行Maven将要求您存储两个路径变量,一个位于jdk1.7_x_x_x位置,也存储在jdk1.7_x_x_ \\ bin中。 If you are using Windows, when you are in your environment variables, do the following: 如果您使用的是Windows,则在环境变量中时,请执行以下操作:

1) create a USER variable called JAVA_HOME. 1)创建一个名为JAVA_HOME的USER变量。 Point this to the location of your JAVA file. 将其指向JAVA文件的位置。 For example: "C:\\Program Files\\Java\\jdk1.7.0_51" (remove the quotes) 例如:“C:\\ Program Files \\ Java \\ jdk1.7.0_51”(删除引号)

2) under the PATH, append %JAVA_HOME% to the PATH. 2)在PATH下,将%JAVA_HOME%附加到PATH。 This will add the file location from step 1 to your PATH. 这会将步骤1中的文件位置添加到PATH中。 This is good for MAVEN 这对MAVEN来说很有用

3) if you are using eclipse you need to have the path point to "C:\\Program Files\\Java\\jdk1.7.0_51\\bin". 3)如果您正在使用eclipse,则需要将路径指向“C:\\ Program Files \\ Java \\ jdk1.7.0_51 \\ bin”。 Now append %JAVA_HOME%\\bin to the end of your path. 现在将%JAVA_HOME%\\ bin附加到路径的末尾。

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 4)你的路径应该是这样的: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. 注意:%%s%中包含的项目分配了变量。 It looks redundant but necessary. 它看起来多余但必要。 You can confirm that everything works by typing in: 您可以输入以下内容确认一切正常:

java -version java -version
javac -version javac -version
mvn -version mvn -version

Each of those three statements typed in comman prompt should not return errors. 在comman提示符中键入的这三个语句中的每一个都不应返回错误。

using windows 10 使用Windows 10

I was facing issue .. then I removed JAVA_HOME variable completly and just added %JAVA_HOME%\\bin in PATH then it worked!!! 我正面临着问题..然后我完全删除了JAVA_HOME变量并在PATH中添加了%JAVA_HOME%\\ bin然后它工作了!!! for mee 对我来说

The JDK has switched locations of java.exe between 1.6 and 1.7!!! JDK已将java.exe的位置切换为1.6到1.7!

In my case I found that the JAVA_HOME for the JDK had to add the \\jre on the end. 在我的例子中,我发现JDK的JAVA_HOME必须在末尾添加\\ jre。 The mvn bat file is looking for java.exe and it looks for it in JAVA_HOME\\bin. mvn bat文件正在寻找java.exe,它在JAVA_HOME \\ bin中查找它。 Its not there for JDK 1.7; 它不适用于JDK 1.7; it is in JAVA_HOME\\jre\\bin. 它在JAVA_HOME \\ jre \\ bin中。 In JDK 1.6 such it IS in JAVA_HOME\\bin. 在JDK 1.6中,它位于JAVA_HOME \\ bin中。

Hope this helps somebody. 希望这有助于某人。

My situation was a bit different. 我的情况有点不同。

  • JAVA_HOME was set properly to point to 1.7 JAVA_HOME设置正确,指向1.7
  • Other Maven projects were working/building fine with 1.7 features. 其他Maven项目正在使用1.7功能正常工作/建设。
  • PATH was set properly. PATH设置正确。
  • Everything was up-to-date. 一切都是最新的。

Still my simple new Maven project was not working. 我的简单的新Maven项目仍然无效。 What I noticed was the difference in the logs when I ran mvn clean install . 我注意到的是当我运行mvn clean install时日志的差异。 For my older Maven projects, it showed 对于我的旧Maven项目,它表明了

[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: 所以,我查看了POM.xml并在旧项目的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. 刚刚将它添加到新项目的POM.xml并且工作正常。

Hope it is useful to someone. 希望它对某人有用。

I am using Windows 7, the problem I had was simple: 我使用的是Windows 7,我遇到的问题很简单:

I had this for my JAVA_HOME environment variable value: 我有这个用于我的JAVA_HOME环境变量值:

"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. 这就是我解决这个问题的方法,所以这应该是一个可能的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM