gpt4 book ai didi

java9 '-release 8' 带有内部包(例如 sun.misc.Unsafe)

转载 作者:行者123 更新时间:2023-12-03 21:17:01 52 4
gpt4 key购买 nike

我正在尝试创建一个 multi-release jar使用 Maven (3.8.0)。我们调用 sun.misc.Unsafe在java8和java11中编译得很好。但是,使用 java11 --release 8 编译引发 Compilation failure: package sun.misc does not exist .

以下是重现错误的方法:

一个调用 Unsafe 的简单类:

public class ChangeableObjects {
sun.misc.Unsafe unsafe;
}

一个简单的pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>8</release>
</configuration>
</plugin>
</plugins>
</build>

如果我们评论 <release>8</release>行,它在 java8 和 java11 上运行良好。

如果我们按原样编译代码,它会以 java8 失败;这是正常的,因为它是 a java9 feature .问题是它在 java11 上也失败了。

我认为问题在于 sun.misc包已在 java8 和 java9 之间移动。但仍然是 release的目的标志是用java8编译好的代码也应该用java11编译好。

我是否误解了如何使用 release旗帜?我需要链接 sun.misc吗?手动打包以使其工作?

最佳答案

首先,如评论中所述,不推荐我构建多版本 jar 的方式(按照我提供的链接)。使用空指针给出的例子,

I would prefer to use something like https://github.com/meterware/multirelease-parent/blob/master/pom.xml



我用过 Maven toolchains ,它允许使用不同的 JDK 进行编译。这样,我的问题就消失了。

其次,为什么我在jdk11中用 --release 8编译时会出现编译错误的答案实际上是由指向 https://stackoverflow.com/a/43103038/296328 的 khmarbaise 给出的:

javac provides two command line options, -source and -target, which can be used to select the version of the Java language accepted by the compiler and the version of the class files it produces, respectively. By default, however, javac compiles against the most-recent version of the platform APIs. The compiled program can therefore accidentally use APIs only available in the current version of the platform. Such programs cannot run on older versions of the platform, regardless of the values passed to the -source and `-target. options. This is a long-term usability pain point, since users expect that by using these options they'll get class files that can run on the specified platform version.



不过,似乎有解决方法:见 Maven: javac: source release 1.6 requires target release 1.6例如。

关于java9 '-release 8' 带有内部包(例如 sun.misc.Unsafe),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53560584/

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