Search code examples
javamaven-2interopnativejna

Generating Java interface from a C++ header file


We have some proprietary libraries we need to interface with. These libraries are Windows DLLs, or Linux .so files. We got the headers to define the interfaces. Since I have never done anything with native libs, I looked at JNAerator (http://code.google.com/p/jnaerator/) and the BridJ and JNA stuff.

What's a simple way to use a C++ header file and the compiled lib to generate an interface? For example, by adopting JNA in general with something like:

SomeDLL lib = (SomeDLL) Native.loadLibrary("some_dll", SomeDLL.class);

I have to keep the DLL somewhere: how do I bundle the DLL with the Jar? I use Maven to build the Jar file... but the Native.loadLibrary interface doesn't allow to directly specify a path.


Solution

  • JNI coding is usually a manual process of writing C++ code to create the native glue methods. There's an entire book that explains it.

    In some cases, http://jna.java.net/ can automate or accelerate this process, but don't count on it.

    You can't 'bundle the native libraries' unless you go down the path of using OSGi or something like the Tanukisoft packaging tool, there's no built-in feature for that purpose in Java.

    You connect the dots by using -Djava.library.path to tell java where to find native libraries, or using the lower-level APIs to System.loadLibrary that allow you to specify a full path.

    Watch out for interactions with PATH and LD_LIBRARY_PATH if your native libraries have dependencies in turn.