无法在 Windows 上安装 Maven:“JAVA_HOME 设置为无效目录”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1184056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to install Maven on Windows: "JAVA_HOME is set to an invalid directory"
提问by hello_world_infinity
I followed the Maven tutorialto 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. Anyone know what the problem is?
所以我确实安装了Java。有谁知道问题是什么?
采纳答案by Stephen C
The problems are to do with your paths.
问题与您的路径有关。
- Make sure that the directory "E:\java resources\apache-maven-2.2.0\bin" is on your command search path.
Make sure that the JAVA_HOME variable refers to the home directoryfor your Java installation. If you are executing Java from "E:\Sun\SDK\jdk\bin", then the JAVA_HOME variable needs to point to "E:\Sun\SDK\jdk".
NB: JAVA_HOME should NOT end with "\bin"1.
Make sure that you haven't put a semicolon in the JAVA_HOME variable2.
NB: JAVA_HOME should be a single directory name, not "PATH-like" list of directory names separated by semicolons.
- 确保目录“E:\java resources\apache-maven-2.2.0\bin”在您的命令搜索路径上。
确保 JAVA_HOME 变量引用Java 安装的主目录。如果从“E:\Sun\SDK\jdk\bin”执行Java,则JAVA_HOME 变量需要指向“E:\Sun\SDK\jdk”。
注意:JAVA_HOME 不应以 "\bin" 1结尾。
确保您没有在 JAVA_HOME 变量2 中放置分号。
注意:JAVA_HOME 应该是单个目录名称,而不是由分号分隔的“类似路径”的目录名称列表。
Also note that you could run into problems if you have ignored this advice in the Maven on Windowsinstructions about spaces in key pathnames.
另请注意,如果您忽略了 Windows 上 Maven 上有关关键路径名中空格的说明中的此建议,则可能会遇到问题。
"Maven, like many cross-platform tools, can encounter problems when there are space characters in important pathnames."
"You need to install the Java SDK (e.g. from Oracle's download site), and you should install it to a pathname without spaces, such as 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 和许多跨平台工具一样,在重要路径名中有空格字符时会遇到问题。”
“您需要安装 Java SDK(例如从 Oracle 的下载站点),并且您应该将其安装到不带空格的路径名,例如 c:\j2se1.6。”'
“您需要解压 Maven 发行版。不要在源代码中间解压它;选择某个位置(路径中没有空格!)并在那里解压。”
The simple remedy for this would be to reinstall Java or Maven in a different location so that there isn'ta space in the path
对此的简单补救措施是在不同的位置重新安装 Java 或 Maven,以便路径中没有空格
1 - .... unless you have made an insanechoice 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. It is not recommended in general, absolutely does not work here.
2 - 显然,Windows 路径问题的常见“巫毒”解决方案是在末尾敲打分号。一般不推荐,这里绝对不行。
回答by dfa
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 设置为无效目录。JAVA_HOME = "E:\Sun\SDK\jdk\bin" 请在您的环境中设置 JAVA_HOME 变量以匹配您的 Java 安装位置
JAVA_HOME
should be set to E:\Sun\SDK\jdk
.
PATH
should be set to include %JAVA_HOME%\bin
.
JAVA_HOME
应设置为E:\Sun\SDK\jdk
.
PATH
应设置为包括%JAVA_HOME%\bin
.
回答by L_7337
I ran into this issue with a Grails install.
我在安装 Grails 时遇到了这个问题。
The problem was my JAVA_HOMEwas c:\sun\jdk\
and my PATHhas %JAVA_HOME%bin
问题是我的JAVA_HOME是c:\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.
在那之后它起作用了。
回答by codinguser
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.
我遇到了同样的问题,上面的答案都没有为我解决这个问题,因为我的 env 变量都已设置。我刚刚重新安装了我的Java。
What worked was to
有效的是
- go to the
C:\path\to\apache-maven-3.0.4\bin
and open themvn.bat
file. - Find the line that looks like this
@SET JAVA_HOME=C:\progra~1\java\jdk1.7.0_03
- Correct it to the right path
- 转到
C:\path\to\apache-maven-3.0.4\bin
并打开mvn.bat
文件。 - 找到看起来像这样的行
@SET JAVA_HOME=C:\progra~1\java\jdk1.7.0_03
- 改正到正确的路径
I don't know if this is Windows specific, but it might help someone!
我不知道这是否特定于 Windows,但它可能对某人有所帮助!
回答by theINtoy
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.
不是最好的解决方案,但它有效。如果有人有任何其他方式或解决方法,其中标准环境变量无法设置到系统中,我欢迎他们的回应。
回答by Debasish Rakshit
JAVA_HOME should be like this C:\PROGRA~1\Java\jdk
JAVA_HOME 应该是这样的 C:\PROGRA~1\Java\jdk
Hope this will work!
希望这会奏效!
回答by Maneshwar
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.
希望,这有助于某人。
回答by Brian Reinhold
The JDK has switched locations of java.exe between 1.6 and 1.7!!!
JDK 在 1.6 和 1.7 之间切换了 java.exe 的位置!!!
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.
就我而言,我发现 JDK 的 JAVA_HOME 必须在末尾添加 \jre。mvn bat 文件正在寻找 java.exe,它在 JAVA_HOME\bin 中寻找它。它不适用于 JDK 1.7;它在 JAVA_HOME\jre\bin 中。在 JDK 1.6 中,它位于 JAVA_HOME\bin 中。
Hope this helps somebody.
希望这可以帮助某人。
回答by Growling Flea
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:
运行 eclipse 并运行 Maven 将要求您存储两个路径变量,一个在 jdk1.7_x_x_x 位置和 jdk1.7_x_x_\bin 中。如果您使用的是 Windows,当您在环境变量中时,请执行以下操作:
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)
1) 创建一个名为 JAVA_HOME 的 USER 变量。将此指向您的 JAVA 文件的位置。例如:“C:\Program Files\Java\jdk1.7.0_51”(去掉引号)
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
2) 在 PATH 下,将 %JAVA_HOME% 附加到 PATH。这会将步骤 1 中的文件位置添加到您的 PATH。这对 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.
3) 如果您使用的是 eclipse,则需要将路径指向“C:\Program Files\Java\jdk1.7.0_51\bin”。现在将 %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. It looks redundant but necessary. You can confirm that everything works by typing in:
注意:用 %'s 括起来的项目(如 %M2%)是分配的变量。它看起来多余但很有必要。您可以通过输入以下内容来确认一切正常:
java -version
javac -version
mvn -version
java -version
javac -version
mvn -version
Each of those three statements typed in comman prompt should not return errors.
在命令提示符下键入的这三个语句中的每一个都不应返回错误。
回答by Bhushan
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.
JAVA_HOME
被正确设置为指向 1.7- 其他 Maven 项目使用 1.7 功能正常工作/构建。
PATH
设置正确。- 一切都是最新的。
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
我的简单的新 Maven 项目仍然不起作用。我注意到的是运行时日志中的差异mvn clean install
。对于我较旧的 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.
希望它对某人有用。