java9 带有内部包的“-release 8”(例如 sun.misc.Unsafe)
java9 '-release 8' with internal packages (e.g. sun.misc.Unsafe)
我正在尝试使用 Maven (3.8.0) 创建 multi-release jar。我们调用 sun.misc.Unsafe
,它在 java8 和 java11 中编译得很好。但是,使用 java11 --release 8
编译会引发 Compilation failure: package sun.misc does not exist
.
重现错误的方法如下:
一个简单的 class 调用 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;这是正常的,因为它是 。问题是它也因 java11.
而失败
我假设问题是 sun.misc
包已在 java8 和 java9 之间移动。但是,release
标志的目的仍然是用 java8 编译得很好的代码也应该用 java11 编译得很好。
我是否误解了如何使用 release
标志?我是否需要手动 link sun.misc
程序包才能使其正常工作?
首先,如评论中所述,不推荐我构建多版本 jar 的方式(遵循我提供的 link)。使用nullpointer给出的例子,
I would prefer to use something like https://github.com/meterware/multirelease-parent/blob/master/pom.xml
我使用了Maven toolchains,它可以使用不同的JDK 进行编译。这样,我的问题就消失了。
其次,为什么我在 jdk11 中使用 --release 8
编译时出现编译错误的答案实际上是由指向 :
的 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.
不过,似乎有解决方法:例如参见 [=15=]。
我正在尝试使用 Maven (3.8.0) 创建 multi-release jar。我们调用 sun.misc.Unsafe
,它在 java8 和 java11 中编译得很好。但是,使用 java11 --release 8
编译会引发 Compilation failure: package sun.misc does not exist
.
重现错误的方法如下:
一个简单的 class 调用 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;这是正常的,因为它是
我假设问题是 sun.misc
包已在 java8 和 java9 之间移动。但是,release
标志的目的仍然是用 java8 编译得很好的代码也应该用 java11 编译得很好。
我是否误解了如何使用 release
标志?我是否需要手动 link sun.misc
程序包才能使其正常工作?
首先,如评论中所述,不推荐我构建多版本 jar 的方式(遵循我提供的 link)。使用nullpointer给出的例子,
I would prefer to use something like https://github.com/meterware/multirelease-parent/blob/master/pom.xml
我使用了Maven toolchains,它可以使用不同的JDK 进行编译。这样,我的问题就消失了。
其次,为什么我在 jdk11 中使用 --release 8
编译时出现编译错误的答案实际上是由指向
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.
不过,似乎有解决方法:例如参见 [=15=]。