javalmdb

How do I open a LMDB database in Java?


I am trying to open a LMDB database using its Java API.

This is my Database class:

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.file.Paths;
import org.lmdbjava.Dbi;
import static org.lmdbjava.DbiFlags.MDB_CREATE;
import org.lmdbjava.Env;
import org.lmdbjava.Txn;

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


}

And my main class:

public class TestApp {

    public static void main(String[] args) {

        Database db = new Database("./data", "DB.TEST");

    }

}

I have double check that directory data exists.

I get the following error:

run:
Exception in thread "main" java.lang.NoClassDefFoundError: jnr/ffi/byref/PointerByReference
    at org.lmdbjava.Env$Builder.open(Env.java:460)
    at org.lmdbjava.Env$Builder.open(Env.java:486)
    at database.Database.<init>(Database.java:41)
    at TestApp.main(TestApp.java:25)
Caused by: java.lang.ClassNotFoundException: jnr.ffi.byref.PointerByReference
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 4 more

This test tries to follow the existing documentation for LMDB Java API:

https://github.com/lmdbjava/lmdbjava/blob/master/src/test/java/org/lmdbjava/TutorialTest.java


Solution

  • The required type is in jnr-ffi-[version].jar.