避免 Eclipse 中多个 Java 版本的 "no JREs installed in the workspace that are strictly compatible" 警告

Avoid the "no JREs installed in the workspace that are strictly compatible" warning in Eclipse for multiple Java versions

我知道已经有很多关于此警告的问题,但我的问题有点不同。我知道我可以在 pom.xml 中使用此配置来修复它

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

这消除了警告,但前提是我在 Eclipse 中安装并配置了 Java 7。例如,如果我只有 Java 8,我仍然会看到警告,除非我将上面配置中的版本更改为 1.8:

Build path specifies execution environment JavaSE-1.7. There are no JREs installed in the workspace that are strictly compatible with this environment.

我的项目与 Java 7、8、9 和 10 兼容。我想要实现的是,如果在不更改版本的情况下安装了 Java 这些版本中的任何一个,则避免此警告在我的 pom.xml 中每次都匹配当前安装的 Java 版本。告诉 Eclipse Java 7 或以上的任何东西都很好。

该项目仍然可以编译并在警告的情况下正常工作,它只是分散注意力,因为它将项目标记为有问题。 我知道我可以在 Eclipse 设置中隐藏这种类型的警告,但是有没有办法只使用 pom.xml 中的一些配置来摆脱它?

Eclipse 使用值 <target>1.7</target> 来决定将哪个执行环境包含在构建路径中,作为其 Maven 集成的一部分。

执行环境映射到安装在 Eclipse 中的 JRE (即在 Preferences > Java > Installed JREs 中引用)。

映射本身在 Preferences > Java > Installed JREs > Runtime Environments 中配置。 Eclipse 会自动将执行环境(例如 JavaSE-1.7)与已安装的 JRE(例如 jdk1.7.0_80)相匹配。如果您的项目中指定的执行环境没有完全匹配,它会给您一个警告,就像您收到的警告一样。

因此,要消除警告,您必须:

  • 安装与您的<target>(即jdk1.7)完全匹配的Java版本并将其添加到Preferences > Java > Installed JREs > Runtime Environments[=42下=]
  • 将 pom 中的 Java 版本配置为完全匹配的版本

My project is compatible with Java 7, 8, 9 and 10. What I want to achieve is to avoid this warning if any of these versions of Java is installed

配置执行环境使用时,Eclipse只关心<target>值。您需要确保在 Eclipse 中安装了 JRE ,其版本与 属性 的值完美匹配。否则,警告不会消失,期间。

I don't have the intention to target some specific version, the default target would work fine for me.

不,不会,因为默认目标是 1.5。目标配置是非可选配置属性,没有'target everything'选项。

您可以从构建路径中删除 Maven 配置的执行环境并直接添加 JRE (project properties > Java Build Path > Libraries > Add library... > JRE System Library > Alternate JRE)。这也会使警告消失,但它会在您 运行 Maven 项目更新后立即恢复。这样的配置不应该真正使用,因为它将您的项目与您机器上的特定安装 Java 联系起来。