gpt4 book ai didi

java - Eclipse Maven 和 Java 8 问题

转载 作者:行者123 更新时间:2023-11-29 06:29:43 25 4
gpt4 key购买 nike

我从简单的网络应用程序转移到 Maven 网络应用程序,使用 Eclipse Neon 我遇到了一个令人沮丧的问题:我在 pom.xml 中添加使用 Java 8 或 7 的规范后,它不起作用。

为了验证它是否有效,我编写了一个简单的类,其中我使用了 try(expression) 声明。

要在 Maven 中使用 Java 8(我已经安装并且它可以在普通 Web 应用程序中运行),我需要做什么?

pom.xml的代码是这样的:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.webdata</groupId>
<artifactId>WebParser</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>WebParser</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

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

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.23</version>
</dependency>
</dependencies>
</project>

最佳答案

首先,您的 JAVA_HOME 必须指向 JDK 1.8。
否则,如果不是这种情况,您必须以这种方式在 pom.xml 的编译器配置中指定 JDK 1.8 作为源和目标:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerVersion>1.8</compilerVersion>
<fork>true</fork>
<executable>D:\jdk1.8\bin\javac</executable>
</configuration>
</plugin>

然后,在 Eclipse 中,您必须检查首选项:

  • Java->Installed JREs 安装了 1.8 JRE/JDK。

如果您想在 Eclipse 中为任何新创建的项目使用 1.8,您可以将以下设置为默认值:

  • Java->Installed JREs:选择 1.8。
  • Java->Compiler :选择 JDK 编译器合规级别为 1.8。

如果默认首选项不使用 1.8 compilation compliance 和 JDK/JRE,或者项目是在此 Eclipse 之外创建的,首选项设置为 1.8 compilation compliance 和 JDK/JRE,您应该检查并可能调整 properties 你的 Eclipse 项目
为此,请进入项目的属性,然后进入 Java Build PathLibraries 选项卡。您必须检查 JRE 系统库 使用 Java 1.8。如果不是这种情况,请将其替换为 1.8 版本。
完成后,始终在项目的属性中,转到 Java Compiler 并检查您使用的 JDK 编译器是否符合 Java 1.8。

它应该可以工作。

关于java - Eclipse Maven 和 Java 8 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38947007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com