Java 无效的目标版本:1.7

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

invalid target release: 1.7

javamavenjava-7

提问by Karan

I have seen similar questions, but haven't yet found the answer.

我看过类似的问题,但还没有找到答案。

Using maven compile, I get:

使用 Maven 编译,我得到:

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

Here is the pom.xml:

这是 pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

Javac version:

Javac版本:

javac 1.7.0_25

Java version:

爪哇版:

java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

Any idea what it could be?

知道它可能是什么吗?

采纳答案by Micha? Nowak

You need to set JAVA_HOME to your jdk7 home directory, for example on Microsoft Windows:

您需要将 JAVA_HOME 设置为您的 jdk7 主目录,例如在 Microsoft Windows 上:

  • "C:\Program Files\Java\jdk1.7.0_40"
  • “C:\Program Files\Java\jdk1.7.0_40”

or on OS X:

或在 OS X 上:

  • /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home
  • /库/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home

回答by Stanley

Other than setting JAVA_HOMEenvironment variable, you got to make sure you are using the correct JDK in your Maven run configuration. Go to Run-> Run Configuration, select your Maven Build configuration, go to JRE tab and set the correct Runtime JRE.

除了设置JAVA_HOME环境变量之外,您还必须确保在 Maven 运行配置中使用正确的 JDK。转到Run-> Run Configuration,选择您的 Maven Build 配置,转到 JRE 选项卡并设置正确的运行时 JRE。

Maven run configuration

Maven 运行配置

回答by djb

When maven is working outside of Eclipse, but giving this error after a JDK change, Go to your Maven Run Configuration, and at the bottom of the Main page, there's a 'Maven Runtime' option. Mine was using the Embedded Maven, so after switching it to use my external maven, it worked.

如果 maven 在 Eclipse 之外工作,但在 JDK 更改后出现此错误,请转到您的 Maven 运行配置,然后在主页底部有一个“Maven 运行时”选项。我使用的是嵌入式 Maven,因此在将其切换为使用我的外部 Maven 后,它起作用了。

回答by user1978019

In IntelliJ IDEA this happened to me when I imported a project that had been working fine and running with Java 1.7. I apparently hadn't notified IntelliJ that java 1.7 had been installed on my machine, and it wasn't finding my $JAVA_HOME.

在 IntelliJ IDEA 中,当我导入一个运行良好并使用 Java 1.7 运行的项目时,这发生在我身上。我显然没有通知 IntelliJ 我的机器上安装了 java 1.7,它没有找到我的$JAVA_HOME.

On a Mac, this is resolved by:

在 Mac 上,这是通过以下方式解决的:

Right clicking on the module | Module Settings | Project

右键单击模块 | 模块设置 | 项目

and adding the 1.7 SDK by selecting "New" in the Project SDK.

并通过在项目 SDK 中选择“新建”来添加 1.7 SDK。

Then go to the main IntelliJ IDEA menu | Preferences | Maven | Runner

然后转到 IntelliJ IDEA 主菜单 | 偏好 | 马文 | 赛跑者

and select the correct JRE. In my case it updated correctly Use Project SDK, which was now 1.7.

并选择正确的 JRE。就我而言,它正确更新了 Use Project SDK,现在是 1.7。

回答by Litome

This probably works for a lot of things but it's not enough for Maven and certainly not for the maven compiler plugin.

这可能适用于很多事情,但对于 Maven 来说还不够,当然对于 Maven 编译器插件来说也不够。

Check Mike's answer to his own question here: stackoverflow question 24705877

在此处查看 Mike 对他自己问题的回答: stackoverflow 问题 24705877

This solved the issue for me both command line AND within eclipse.

这为我解决了命令行和 eclipse 中的问题。

Also, @LinGao answer to stackoverflow question 2503658and the use of the $JAVACMD variable might help but I haven't tested it myself.

另外,@LinGao 对stackoverflow 问题2503658 的回答以及 $JAVACMD 变量的使用可能会有所帮助,但我自己还没有测试过。