为什么Java 11运行时会忽略我的包含sun.misc类的jar?

Vin*_*ray 9 java java-11

我正在尝试将代码库升级到Java11。不幸的是,我的代码依赖于内部使用sun.misc.BASE64Encoder和的第三方库Decoder。由于该sun.misc软件包已从Java 11 JRE中删除,因此出现了故障。该库的所有者尚未替换该依赖项,因此我坚持了一段时间。

如果可以控制代码,则可以使用java.util.BASE64类,但是正如我所说的那样,这些类是作为另一个库的可传递依赖项而来的,因此我无法更改。

我以为我会很聪明,只用这些类创建一个新的jar,但是由于某种原因,该jar被忽略了。

在此处输入图片说明

<dependency>
    <groupId>sun.misc</groupId>
    <artifactId>BASE64</artifactId>
    <version>1.8</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/sun.jar</systemPath>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我也尝试过将其显式添加到类路径中,但还是没有运气

这是JRE阻止您使用的那些软件包之一,还是我错过了某些模块规范,或者这是一个节目表演者吗?

这是输出

java.lang.NoClassDefFoundError: sun/misc/BASE64Encoder
at com.propsco.util.support.PropsLoader.save(PropsLoader.java:478) ~[props-client-2.2.1.jar:na]
Run Code Online (Sandbox Code Playgroud)

Ada*_*lik 5

这个答案是用

> java --version
openjdk 11.0.3 2019-04-16
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.3+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.3+7, mixed mode)
Run Code Online (Sandbox Code Playgroud)

首先,您将创建具有重写的项目BASE64Encoder。叫它sun-misc-override。在类下src\main\java创建sun.miscBASE64Encoder

package sun.misc;

public class BASE64Encoder {
    public String encode(byte[] aBuffer) {
        return "Fake it until you make it!";
    }
}
Run Code Online (Sandbox Code Playgroud)

如果尝试对其进行编译,则会出现sun\misc\BASE64Encoder.java:1: error: package exists in another module: jdk.unsupported错误。

这提示我们需要修补模块 jdk.unsupported。这是因为在Java 9中推出模块系统后,原始sun.misc包中的类已移至该jdk.unsupported模块,然后随着时间的流逝而被移除(请参阅JEP-260)。

使用Maven,您可以像这样配置编译器插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <release>11</release>
        <compilerArgs>
            <arg>--patch-module</arg>
            <arg>jdk.unsupported=${project.basedir}/src/main/java</arg>
        </compilerArgs>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

com.example:sun-misc-override:1.0.0-SNAPSHOT构建完成后,将生成的JAR放入“主”项目中-就像您所做的一样。在lib目录中。我还没有找到一种使其与常规Maven依赖项一起工作的方法。

现在,在“主”项目中配置编译器插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <release>11</release>
        <compilerArgs>
            <arg>--patch-module=jdk.unsupported=${project.basedir}/lib/sun-misc-override-1.0.0-SNAPSHOT.jar</arg>
        </compilerArgs>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

(可能是由于MCOMPILER-311,当我尝试使用NPE时

<compilerArgs>
    <arg>--patch-module</arg>
    <arg>jdk.unsupported=${project.basedir}/lib/sun-misc-override-1.0.0-SNAPSHOT.jar</arg>
</compilerArgs>
Run Code Online (Sandbox Code Playgroud)

即使该错误应该通过maven-compiler-plugin 3.8.0修复,并且在。的POM中也可以正常工作sun-misc-override

现在,我的“主”项目被调用j11并具有一个类:

package com.example;

import sun.misc.BASE64Encoder;

public class BASE64EncoderTest {
    public static void main(String[] args) {
        System.out.println("Testing - " + new BASE64Encoder().encode(new byte[0]));
    }
}
Run Code Online (Sandbox Code Playgroud)

要运行它,您需要--patch-module再次指定:

> java --patch-module=jdk.unsupported=lib\sun-misc-override-1.0.0-SNAPSHOT.jar -cp target\j11-1.0.0-SNAPSHOT.jar com.example.BASE64EncoderTest
Testing - Fake it until you make it!
Run Code Online (Sandbox Code Playgroud)