簡體   English   中英

java 9 中介紹的方法是使用編譯器目標 java 8 編譯的

[英]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 將 package 它放入 docker 容器中時,它會啟動,但是當我調用該方法時會拋出以下異常:

java.lang.NoSuchMethodError: java.time.LocalTime.ofInstant(Ljava/time/Instant;Ljava/time/ZoneId;)Ljava/time/LocalTime

我怎樣才能避免和識別這些情況,然后再進行 go 測試? 我做錯了什么還是構建系統或JDK11中的錯誤?

提前致謝

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得到像你一樣的錯誤。

有兩個很好的解決方案。 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.

另一種是使用新版本和 testRelease 選項。 他們將使用給定版本中的 API 類進行構建。 只需添加<release>1.8</release>

如果您使用的是 JDK 11,請像這樣配置您的 maven pom.xml

 <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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM