简体   繁体   English

无法让Maven识别Java 1.8

[英]Can't get Maven to recognize Java 1.8

I can't seem to be able to get Maven to use Java 1.8. 我似乎无法让Maven使用Java 1.8。 Using 1.8 as the target turns up the following error: 使用1.8作为目标会出现以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
(default-compile) on project csaro: Fatal error compiling: invalid target
release: 1.8 -> [Help 1]

The cause of the error is obvious enough: Maven isn't using the right version of Java: 错误的原因很明显:Maven没有使用正确版本的Java:

$ mvn -version
Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T07:51:42-06:00)
Maven home: /usr/local/Cellar/maven/3.2.2/libexec
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.2", arch: "x86_64", family: "mac"

But the installed version of Java should be 1.8: 但是安装的Java版本应该是1.8:

$ java -version
java version "1.8.0_20-ea"
Java(TM) SE Runtime Environment (build 1.8.0_20-ea-b22)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b21, mixed mode)

And JAVA_HOME is set: 并设置JAVA_HOME:

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home

I also tried the command here (which creates mavenrc ). 我也试过这里命令 (创建mavenrc )。 I've tried restarting the computer several times, and have verified that the env var is correctly set (it's set in .bash_profile ). 我已经尝试多次重启计算机,并验证了env var是否正确设置(它设置在.bash_profile )。

Maven was installed with Homebrew. Maven安装了Homebrew。

Java 1.8 is working fine in Eclipse (which is using m2e). Java 1.8在Eclipse中运行良好(使用m2e)。 I just can't get Maven to work on the command line. 我只是不能让Maven在命令行上工作。

For me the issue was that when I installed from jdk-8u25-macosx-x64.dmg the installation did not update the /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK directory link to the new 1.8 JDK. 对我来说问题是当我从jdk-8u25-macosx-x64.dmg安装时,安装没有将/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK目录链接更新到新的1.8 JDK。 This may not have been the issue of the original question but it was the solution for me when I encountered the same error message. 这可能不是原始问题的问题,但当我遇到相同的错误消息时,它是我的解决方案。

I don't know why the installer doesn't link it and to make matters more confusing, the path is different depending on whether you installed it from Oracle or Apple. 我不知道为什么安装程序没有链接它并使问题更加混乱,路径是不同的,这取决于你是从Oracle还是Apple安装它。 See Mac OS X 10.6.7 Java Path Current JDK confusing 请参阅Mac OS X 10.6.7 Java Path当前JDK令人困惑

I did the following to fix my environment 我做了以下修复我的环境

cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo rm CurrentJDK
sudo ln -s /Library/Java/JavaVirtualMachines/<installed_jdk_version>.jdk/Contents CurrentJDK

It turns out that I had a forgotten ~/.mavenrc file that had set the value of JAVA_HOME . 事实证明我有一个被遗忘的~/.mavenrc文件,它设置了JAVA_HOME的值。

For future readers, check the following locations for places that may override JAVA_HOME (in ascending order of precedence): 对于将来的读者,请检查以下位置以查找可能覆盖JAVA_HOME (按优先级升序排列):

  • ~/.bash_profile
  • ~/.bash_login
  • ~/.bashrc
  • ~/.profile
  • /etc/mavenrc
  • ~/.mavenrc

The solution which worked for me: 对我有用的解决方案:

In Eclipse, I had a wrong run configuration in my Maven build. 在Eclipse中,我的Maven构建中的运行配置有误。 Go to

Run Configurations -> JRE [Tab] 运行配置 - > JRE [Tab]

and set the runtime to Java 8. 并将运行时设置为Java 8。

I ran into the same problem on my CentOS 6.5 system. 我在CentOS 6.5系统上遇到了同样的问题。

java -version returned a Java 8 version, but javac -version returned a Java 7 version. java -version返回了Java 8版本,但javac -version返回了Java 7版本。

I had to run the following commands to get all kinds of Java-related symlinks point to my JDK 8 installation: 我必须运行以下命令才能使各种与Java相关的符号链接指向我的JDK 8安装:

sudo alternatives --config java
sudo alternatives --config javac
sudo alternatives --config jre_openjdk
sudo alternatives --config java_sdk_openjdk

Your JAVA_HOME is being overridden from somewhere else. 您的JAVA_HOME正在被其他地方覆盖。 Try echoing in mvn right before java invocation. 在java调用之前尝试在mvn回显。

Note that you can also get the "invalid target release" error with Java 11 请注意,您还可以使用Java 11获取“无效目标发布”错误

 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler- plugin:3.8.0:compile (default-compile) on project projects-migration: Fatal error compiling: error: invalid target release: 1.11 -> [Help 1] 

if you set 1.11 instead of just 11 in the POM. 如果你在POM中设置1.11而不是11

Incorrect: 不正确:

<java.version>1.11</java.version>

Correct: 正确:

<java.version>11</java.version>

I had same problem, needed 2 steps to make correct: 我有同样的问题,需要两个步骤来纠正:

  1. Ensure JAVA_HOME is set pointing correctly to JDK; 确保JAVA_HOME为正确指向JDK;
  2. JDK and JRE in eclipse should point the same version. eclipse中的JDK和JRE应该指向相同的版本。

For me 1 step was right, 2nd step had problem. 对我来说,一步是对的,第二步有问题。 In eclipse » Project's properties » Build Path » Installed JRE » point to JDK. 在eclipse中»项目的属性»构建路径»已安装的JRE»指向JDK。

You are using an early access release of 1.8. 您正在使用1.8的早期访问版本。 Now that is has been released, you should install the released version. 现在已经发布,您应该安装已发布的版本。

Check you maven installation to see if you have a configuration associated with it that specifies a particular JDK. 检查maven安装,看看是否有与之关联的配置,指定特定的JDK。 Occasionally there are wrapper scripts which reset JAVA_HOME prior to maven launch, or the maven executable is launched in a wrapper that refers to a config file that could bind you to a particular JVM. 偶尔会有包装脚本在maven启动之前重置JAVA_HOME,或者maven可执行文件在包装器中启动,该包装器引用可以绑定到特定JVM的配置文件。

If you are using Maven within a continuous integration server be sure to check it's settings as it may override the JDK used by Maven. 如果您在持续集成服务器中使用Maven,请务必检查它的设置,因为它可能会覆盖Maven使用的JDK。 For Jenkins, I had to add the new 1.8 JDK in the general settings and configure my project to use it. 对于Jenkins,我必须在常规设置中添加新的1.8 JDK并配置我的项目以使用它。

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

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