I'm new to Java and have been coding in JDK 11, but now using JDK 16. When I try to open a database connection method from the Spring Boot main class, by calling the database connection class, even when it is inside the same package, I get this error:
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
I researched and found JDK 16 may require modular definitions to be able to access classes outside its own class, so I set up a module-info.java file in the package root and added all the requires
and exports
, but this throws further errors that some classes are not accessible by others, so I have deleted it for now.
I'm reluctant to use this hack in the JVM: https://nipafx.dev/five-command-line-options-hack-java-module-system/
Any advice on how to open a LevelDB connection from an outside class? I am missing something very simple?
EDIT:
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 MAIN 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();
}
}
DATABASE CONNECTION 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;
}
}
Turns out that the solution was to upgrade to the latest version of LevelDb which has resolved the issue:
<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>