Java 16 模块化 IllegalAccessException Java.NIO Spring 启动

Java 16 Modularisation IllegalAccessException Java.NIO Spring Boot

我是 Java 的新手,一直在 JDK 11 编码,但现在使用 JDK 16。当我尝试从 [= 打开数据库连接方法时54=] Boot main class,通过调用数据库连接 class,即使它在同一个包中,我也会收到此错误:

java.lang.reflect.InaccessibleObjectException: Unable to make public jdk.internal.ref.Cleaner java.nio.DirectByteBuffer.cleaner() accessible: module java.base does not "opens java.nio" to unnamed module

我研究发现 JDK 16 可能需要模块化定义才能访问其自身 class 之外的 classes,所以我设置了一个模块-info.java 文件并添加了所有 requiresexports,但这会引发进一步的错误,即某些 classes 无法被其他人访问,因此我暂时将其删除。

我不愿意在 JVM 中使用这个技巧: https://nipafx.dev/five-command-line-options-hack-java-module-system/

关于如何从外部打开 LevelDB 连接的任何建议 class?我错过了一些非常简单的东西?

编辑:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>java16-leveldb</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.iq80.leveldb</groupId>
            <artifactId>leveldb-api</artifactId>
            <version>0.9</version>
        </dependency>
        <dependency>
            <groupId>org.iq80.leveldb</groupId>
            <artifactId>leveldb</artifactId>
            <version>0.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

SPRING 主要 CLASS:

package io.strata.node.stratanode;

import io.strata.node.stratanode.database.DatabaseConnection;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StrataNodeApplication {

    public static void main(String[] args) {
        SpringApplication.run(StrataNodeApplication.class, args);
        DatabaseConnection databaseConnection = new DatabaseConnection();

    }

}

数据库连接CLASS:

package io.strata.node.stratanode.database;

import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBFactory;
import org.iq80.leveldb.Options;
import org.iq80.leveldb.impl.Iq80DBFactory;

import java.io.File;
import java.io.IOException;

public class DatabaseConnection {

    public DatabaseConnection() {
        openDatabaseConnection("test");
    }

    private DB database;
    private DBFactory factory = new Iq80DBFactory();

    public boolean openDatabaseConnection(String filePath) {

        Options options = new Options();
        options.createIfMissing(true);

        try {
            database = factory.open(new File(filePath), options);
            System.out.println(filePath + " has been created");
            return true;

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(filePath + " database could not be opened/created");
        return false;
    }
}

事实证明,解决方案是升级到最新版本的 LevelDb,问题已解决:

        <dependency>
            <groupId>org.iq80.leveldb</groupId>
            <artifactId>leveldb-api</artifactId>
            <version>0.12</version>
        </dependency>
        <dependency>
            <groupId>org.iq80.leveldb</groupId>
            <artifactId>leveldb</artifactId>
            <version>0.12</version>
        </dependency>
        <dependency>