我的系统中有以下配置:
Apache maven 3.5.2 Maven 主页:/usr/share/maven Java版本:1.8.0_162,供应商:Oracle Corporation Java 主页:/usr/lib/jvm/java-8-openjdk-amd64/jre 默认语言环境:en_US,平台编码:UTF-8 操作系统名称:“linux”,版本:“4.15.0-20-generic”,arch:“amd64”,系列:“unix”
当我编译(使用 maven)具有 Try-with-Resources 的项目时,出现以下错误:
/path/driver.java:[29,13] try-with-resources is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable try-with-resources)
我尝试添加标志 -source 7 但这不是解决问题的方法,因为它给我另一个错误:
[ERROR] Error executing Maven.
[ERROR] The specified user settings file does not exist: /path/ource
我在互联网上搜索了第一个错误,但我没有找到任何东西
最佳答案
如果你想使用 Java 8 语言特性(-source 1.8),同时希望编译后的类兼容 JVM 1.8(-target 1.8),你可以添加以下两个属性,这是默认属性插件参数的名称:
<project>
[...]
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
[...]
</project>
或者直接配置插件:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
看看这个 article了解更多详情。然而 try-with-resources
是在 JDK 1.7(内部)版本中引入的。
关于java - Maven编译报错try-with-resources,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50271414/