英文:
Method introduced in java 9 is compiled using compiler target java 8
问题
我在Maven和Eclipse本身中遇到了奇怪的行为。尽管我已将项目配置为在Java 1.8中编译,但我却可以编译并运行(在Eclipse中)一个在Java 9中引入的代码片段。
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
有问题的代码行:
LocalTime.ofInstant(cal.toInstant(), cal.getTimeZone().toZoneId());
我在本地使用Oracle的JDK 11进行编译和在Eclipse中运行,没有任何错误。但当我使用openjdk:8-jdk-alpine将其打包到Docker容器中时,容器会启动,但在调用该方法时会抛出以下异常:
java.lang.NoSuchMethodError: java.time.LocalTime.ofInstant(Ljava/time/Instant;Ljava/time/ZoneId;)Ljava/time/LocalTime
在这些情况在进入测试之前,我该如何避免并识别这些问题?我是做错了什么,还是构建系统或JDK 11中有bug?
提前谢谢。
英文:
I'm experiencing an odd behavior in maven as well as in eclipse itself.
Even though i configured my project to be compiled in Java 1.8, I can compile and run (eclipse) a piece of code that was introduced in Java 9
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
The code line in question:
LocalTime.ofInstant(cal.toInstant(), cal.getTimeZone().toZoneId());
I'm using Oracle's JDK 11 locally for compiling and running in eclipse without any errors. When i package it into a docker container using openjdk:8-jdk-alpine it will boot up, but throw the following Exception when I call the method:
java.lang.NoSuchMethodError: java.time.LocalTime.ofInstant(Ljava/time/Instant;Ljava/time/ZoneId;)Ljava/time/LocalTime
How can I avoid and identify these situations before they go to testing? Am I doing something wrong or is it a bug in the build system or in JDK11?
thanks in advance
答案1
得分: 3
源选项指定源代码必须与Java 8兼容,目标选项指定类应与Java 8兼容。然而,如果您使用Java 11构建并且使用Java 11类库进行编译,则仍将编译为Java 11类库,这可能会导致出现类似您遇到的错误。
有两个很好的解决方案。一个是使用Maven工具链插件,并使用Java 8进行构建。然后您可以安装多个Java版本,Maven将在每个项目的基础上使用配置的版本。
另一个解决方案是使用新的release和testRelease选项。它们将使用给定发布版本的API类进行构建。只需添加 <release>1.8</release>
。
英文:
The source option specifies that the source code must be compatible with Java 8, the target option that the classes should be compatible with Java 8. However, you will still compile with the Java 11 class library if you build with Java 11 and then you can get errors like the one you have.
There are two good solutions. One is to use the Maven toolchains plugin and build with Java 8. Then you can have multiple Java versions installed and Maven will use the configured one on a per-project basis.
The other is to use the new release and testRelease options. They will build with API classes from the given release. Just add <release>1.8</release>
.
答案2
得分: 1
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>
英文:
If you are using JDK 11, configure your maven pom.xml
like that:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论