Java Maven:编译致命错误:目标版本无效:1.8

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

Maven: Fatal error compiling: invalid target release: 1.8

javamavenjava-8

提问by Pankaj

We just upgraded the project from jdk 1.6to jdk 1.8. While building the project in my machine, i'm getting following error.

我们刚刚将项目从 升级jdk 1.6jdk 1.8. 在我的机器上构建项目时,出现以下错误。

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

[错误] 无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project exception: Fatal error compiling: invalid target release: 1.8 -> [Help 1]

Here is the maven compiler plugin which is been used

这是使用的maven编译器插件

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        </configuration>
  </plugin>

I looked at the many post and most of them were related with not having the correct version configured for java_home. I verified all of these aspects and couldn't find any of them alarming.

我看了很多帖子,其中大部分都与没有为 java_home 配置正确的版本有关。我验证了所有这些方面,但没有发现任何令人担忧的方面。

Java version -

Java版——

 qadeersmsiphone:main pdubey$ java -version
 java version "1.8.0_51"
 Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
 Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

Maven Version -

Maven 版本 -

qadeersmsiphone:main pdubey$ mvn -version
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 08:22:22-0700)
Maven home: /usr/share/maven
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.5", arch: "x86_64", family: "mac"

and I checked that while building the project maven uses jdk 1.8

我检查了在构建项目时 maven 使用 jdk 1.8

qadeersmsiphone:main pdubey$ mvn install -debug
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 08:22:22-0700)
Maven home: /usr/share/maven
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.5", arch: "x86_64", family: "mac"
[INFO] Error stacktraces are turned on.
[DEBUG] Reading global settings from /usr/share/maven/conf/settings.xml
[DEBUG] Reading user settings from /Users/pdubey/.m2/settings.xml
[DEBUG] Using local repository at /Users/pdubey/.m2/repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for  /Users/pdubey/.m2/repository
[INFO] Scanning for projects...

UPDATE:I could get it working by removing the maven-compiler-plugin in pom file (Ideally, i don't want to do this). And i also observed that even if i remove this plugin, maven by default downloads the 3.3 version of compiler plugin in my local repo. I'm not sure what's wrong in this plugin although source and target appears to be correct.

更新:我可以通过删除 pom 文件中的 maven-compiler-plugin 来让它工作(理想情况下,我不想这样做)。而且我还观察到,即使我删除了这个插件,maven 默认会在我的本地存储库中下载 3.3 版本的编译器插件。虽然源和目标似乎是正确的,但我不确定这个插件有什么问题。

采纳答案by Pankaj

Check the java and Javac version - My Java version was 1.8.0_51

检查 java 和 Javac 版本 - 我的 Java 版本是 1.8.0_51

$ java -version
java version "1.8.0_51"

But javac version was pointing to old jdk 1.7.0_67

但是 javac 版本指向旧的 jdk 1.7.0_67

$ javac -version
javac 1.7.0_67

There are couple of thing which we should try -

有几件事我们应该尝试 -

  1. Ensure that symbolic link is pointing to correct jdk. If not, remove existing link and create new one for your jdk

    cd /System/Library/Frameworks/JavaVM.framework/Versions/
    rm CurrentJDK
    ln -s /Library/Java/JavaVirtualMachines/<jdk version>.jdk/Contents/ CurrentJDK
    
  2. In /Library/Java/Extension, i had a tools.jar. I believe, this was from old jdk.

    cd /Library/Java/Extensions
    -rw-r--r--  1 root  admin  15286304 Jan 14  2015 tools.jar
    

    Remove this

    sudo rm -rf /Library/Java/Extensions
    
  1. 确保符号链接指向正确的 jdk。如果没有,请删除现有链接并为您的 jdk 创建新链接

    cd /System/Library/Frameworks/JavaVM.framework/Versions/
    rm CurrentJDK
    ln -s /Library/Java/JavaVirtualMachines/<jdk version>.jdk/Contents/ CurrentJDK
    
  2. 在/Library/Java/Extension,我有一个tools.jar。我相信,这是来自旧的 jdk。

    cd /Library/Java/Extensions
    -rw-r--r--  1 root  admin  15286304 Jan 14  2015 tools.jar
    

    删除这个

    sudo rm -rf /Library/Java/Extensions
    

point 2 did the trick for me :)

第2点对我有用:)

回答by Adelina

I had the same issue:

我遇到过同样的问题:

> java -version
java version "1.8.0_111"

>javac -version
javac 1.7.0.6

I checked the Path in System variables, and the jdk was not updated to jdk 8. After I updated the path with the right jdk it worked.

我检查了系统变量中的路径,jdk 没有更新到 jdk 8。在我用正确的 jdk 更新路径后,它工作了。

回答by Lord Nighton

Sometimes this problem is related to a JAVA_HOMEused by maven.

有时这个问题与JAVA_HOMEmaven使用的一个有关。

  • In my particular case I tried to start an installtarget and had an exact the subj message.
  • Then I just tried an mvn -versioncommand and I realized that it uses jdk7
  • Then I run in a console set JAVA_HOME=C:\Java\jdk8(my jdk path, so your own could be different)
  • Then typed mvn clean install testand it worked just fineat last
  • 在我的特殊情况下,我尝试启动一个install目标并得到一个确切的 subj 消息。
  • 然后我只是尝试了一个mvn -version命令,我意识到它使用 jdk7
  • 然后我在控制台中运行set JAVA_HOME=C:\Java\jdk8(我的 jdk 路径,所以你自己的可能会有所不同)
  • 然后打字,最后mvn clean install test它工作得很好

Good luck!

祝你好运!

回答by Manu Pillai

Same issue here, but I was using run configurations inside Eclipse when maven kept throwing this error. Fixed by setting the right JDK version here:

这里有同样的问题,但是当 maven 不断抛出这个错误时,我在 Eclipse 中使用了运行配置。通过在此处设置正确的 JDK 版本来修复:

Run Configuration image

运行配置映像

回答by Kanagavelu Sugumar

In cent os we can use alternatives to switch the version.

在 cent os 中我们可以使用替代品来切换版本。

alternatives --config javac
alternatives --config java

But MAVEN uses JAVA_HOME.

但是 MAVEN 使用 JAVA_HOME。

And this JAVA_HOME can be possible to a link.

和这个JAVA_HOME 可以有一个链接。

Either we have to point the JAVA_HOME manually to new jdk folder, OR manually remove/delete the link and create a new same link again from above answers.

我们必须手动将 JAVA_HOME 指向新的 jdk 文件夹,或者手动删除/删除链接并从上述答案中再次创建一个新的相同链接。

From alternatives we can get the path of jdk

从替代方案我们可以得到jdk的路径

回答by Kostiantyn

First of all, it is definitely versions issue.

首先,这绝对是版本问题。

In my situation: java -version showed the result

在我的情况下: java -version 显示了结果

java version "1.8.0_172"

and javac -version was showing

和 javac -version 显示

javac 1.8.0_172

BUT: Maven uses JAVA_HOME environment variable to get your JDK version to build a project and run it's a plugin. So the way how we have to check it is

但是:Maven 使用 JAVA_HOME 环境变量来获取您的 JDK 版本以构建项目并运行它的插件。所以我们必须检查它的方式是

echo %JAVA_HOME%

Which in my situation was

在我的情况下是

jdk1.7.0_172

So simply change the path to the variable will solve the issue. Hope it will save time to someone.

因此,只需更改变量的路径即可解决问题。希望它会为某人节省时间。