6

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();
    }
}
Al.G.
  • 4,327
  • 6
  • 31
  • 56
  • Which java version are you using? – Al.G. Nov 10 '21 at 12:44
  • 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. – Vishnu Chithan Nov 11 '21 at 16:24
  • This is a known issue related to a backwards-incompatible change in Java itself. See https://github.com/lmdbjava/lmdbjava/issues/42 – BadZen Jun 06 '22 at 16:15
  • In general, don't use lmdb with a non-supported Java platform. There are a bunch of little gotchas like this. – BadZen Jun 09 '22 at 21:42

1 Answers1

6

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

axiopisty
  • 4,972
  • 8
  • 44
  • 73
  • This is lmdb, it's not a relational database and there is no JDBC driver involved. – BadZen Jun 06 '22 at 16:12
  • 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. – axiopisty Jun 06 '22 at 21:03