Vishnu Chithan
Vishnu Chithan

Reputation: 156

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

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();
    }
}

Upvotes: 13

Views: 17931

Answers (2)

Avik Ray
Avik Ray

Reputation: 109

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

Upvotes: 1

axiopisty
axiopisty

Reputation: 5137

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

Upvotes: 12

Related Questions