无法让 Maven 识别 Java 1.8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24705877/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:18:18  来源:igfitidea点击:

Can't get Maven to recognize Java 1.8

javamaven

提问by Kat

I can't seem to be able to get Maven to use Java 1.8. Using 1.8 as the target turns up the following error:

我似乎无法让 Maven 使用 Java 1.8。使用 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). I've tried restarting the computer several times, and have verified that the env var is correctly set (it's set in .bash_profile).

我还尝试了此处命令(创建了mavenrc)。我试过多次重新启动计算机,并验证了 env var 设置是否正确(在 中设置.bash_profile)。

Maven was installed with Homebrew.

Maven 与 Homebrew 一起安装。

Java 1.8 is working fine in Eclipse (which is using m2e). I just can't get Maven to work on the command line.

Java 1.8 在 Eclipse(使用 m2e)中运行良好。我只是无法让 Maven 在命令行上工作。

采纳答案by Kat

It turns out that I had a forgotten ~/.mavenrcfile 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
  • ~/.bash_profile
  • ~/.bash_login
  • ~/.bashrc
  • ~/.profile
  • /etc/mavenrc
  • ~/.mavenrc

回答by Edwin Buck

You are using an early access release of 1.8. Now that is has been released, you should install the released version.

您使用的是 1.8 的抢先体验版。现在已经发布了,你应该安装发布的版本。

Check you maven installation to see if you have a configuration associated with it that specifies a particular 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 安装,看看您是否有与之关联的配置,该配置指定了特定的 JDK。有时,有一些包装脚本会在 maven 启动之前重置 JAVA_HOME,或者 maven 可执行文件在包装器中启动,该包装器引用了可以将您绑定到特定 JVM 的配置文件。

回答by Jigar Joshi

Your JAVA_HOMEis being overridden from somewhere else. Try echoing in mvnright before java invocation.

JAVA_HOME正在从其他地方被覆盖。尝试mvn在 java 调用之前立即回显。

回答by Kirby

For me the issue was that when I installed from jdk-8u25-macosx-x64.dmgthe installation did not update the /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDKdirectory link to the new 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.

对我来说,问题是当我从jdk-8u25-macosx-x64.dmg安装安装时没有将/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK目录链接更新到新的 1.8 JDK。这可能不是原始问题的问题,但是当我遇到相同的错误消息时,它是我的解决方案。

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. See Mac OS X 10.6.7 Java Path Current JDK confusing

我不知道为什么安装程序没有链接它,更令人困惑的是,根据您是从 Oracle 还是 Apple 安装它,路径会有所不同。请参阅Mac OS X 10.6.7 Java Path Current 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

回答by Bevor

The solution which worked for me:

对我有用的解决方案:

In Eclipse, I had a wrong run configuration in my Maven build. Go to

在 Eclipse 中,我的 Maven 构建中有错误的运行配置。去

Run Configurations -> JRE [Tab]

运行配置 -> JRE [Tab]

and set the runtime to Java 8.

并将运行时设置为 Java 8。

回答by Abdull

I ran into the same problem on my CentOS 6.5 system.

我在 CentOS 6.5 系统上遇到了同样的问题。

java -versionreturned a Java 8 version, but javac -versionreturned 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

回答by Henno Vermeulen

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. For Jenkins, I had to add the new 1.8 JDK in the general settings and configure my project to use it.

如果您在持续集成服务器中使用 Maven,请务必检查它的设置,因为它可能会覆盖 Maven 使用的 JDK。对于 Jenkins,我必须在常规设置中添加新的 1.8 JDK 并配置我的项目以使用它。

回答by sandeep pandey

I had same problem, needed 2 steps to make correct:

我有同样的问题,需要 2 个步骤来纠正:

  1. Ensure JAVA_HOMEis set pointing correctly to JDK;
  2. JDK and JRE in eclipse should point the same version.
  1. 确保JAVA_HOME设置正确指向 JDK;
  2. 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.

对我来说第一步是正确的,第二步有问题。在日食?项目属性 ? 构建路径?安装了 JRE 吗?指向JDK。

回答by Andrew Spencer

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]
[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.11instead of just 11in the POM.

如果你设置1.11而不是仅仅11在 POM 中。

Incorrect:

不正确:

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

Correct:

正确的:

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