Unable to make field long java.nio.Buffer.address accessible: module java.base does not "opens java.nio" - LMDB using Java API
Asked Answered
M

2

13

I'm trying to create DB and env using LMDB. I'm facing an issue on Env. create(). I have used LMDB documentation for this.

Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.lmdbjava.ByteBufferProxy.<clinit>(ByteBufferProxy.java:71)
    at org.lmdbjava.Env.create(Env.java:92)
    at Database.<init>(Database.java:23)
    at Index.main(Index.java:7)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field long java.nio.Buffer.address accessible: module java.base does not "opens java.nio" to unnamed module @4edde6e5
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:177)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:171)
    at org.lmdbjava.ByteBufferProxy$AbstractByteBufferProxy.findField(ByteBufferProxy.java:163)
    at org.lmdbjava.ByteBufferProxy$ReflectiveProxy.<clinit>(ByteBufferProxy.java:222)
    ... 4 more

Main:

public class Index {
    public static void main(String[] args) {
        Database db = new Database("./data", "DB.TEST");
    }
}

public class Database {

    private String dbName;
    private String dbDirectoryName;
    private File dbDirectory;
    private Env<ByteBuffer> dbEnvironment;
    private Dbi<ByteBuffer> db;

    public Database(String _dbDirectoryName, String _dbName) {

        dbName = _dbName;
        dbDirectoryName = _dbDirectoryName;
        dbDirectory = new File(dbDirectoryName);

        dbEnvironment = Env.create().setMapSize(1_073_741_824).setMaxDbs(1).open(dbDirectory);
        db = dbEnvironment.openDbi(dbName, MDB_CREATE);

    }

    public void Close() {
        dbEnvironment.close();
    }
}
Mcgee answered 26/10, 2021 at 18:34 Comment(4)
Which java version are you using?Yoicks
In Java 16, the issue is happening. On Changing to Java 1.8 working fine. Still wanted to know how to resolve it without downgrading the version.Mcgee
This is a known issue related to a backwards-incompatible change in Java itself. See github.com/lmdbjava/lmdbjava/issues/42Shirashirah
In general, don't use lmdb with a non-supported Java platform. There are a bunch of little gotchas like this.Shirashirah
N
12

The problem has to do with compatibility issues between the LMDB library you're using and the JRE you're using. Java 9 introduced the JPMS, the Java Platform Module System. The error message you provided in the OP indicates that your application is running in a JRE version 9 or higher, but the LMDB library you're using is probably compiled for Java 8.

You have the option to instruct the JPMS to load classes in the unnamed module using the --add-opens option.

For the specific error message in the OP, you can try adding this option to the command you are using to run your application:

--add-opens=java.base/java.nio=ALL-UNNAMED

See the section for add-opens here: https://docs.oracle.com/en/java/javase/16/migrate/migrating-jdk-8-later-jdk-releases.html#GUID-12F945EB-71D6-46AF-8C3D-D354FD0B1781

See also: https://blogs.oracle.com/javamagazine/post/its-time-to-move-your-applications-to-java-17-heres-why-and-heres-how

enter image description here

Narial answered 3/6, 2022 at 11:4 Comment(2)
This is lmdb, it's not a relational database and there is no JDBC driver involved.Shirashirah
Thanks. I updated my answer to remove references to JDBC. Regardless, the main points and contexts of this answer is the answer to to OP.Narial
B
1

Due to the reasons mentioned in axiopisty's answer, you will need to add few JVM args from the list here: https://github.com/apache/ignite/issues/10747#issuecomment-1566646439

Brawl answered 12/1 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.