Creating a Java Interface from a C ++ Header File

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

What is an easy way to use a C ++ header file and compiled lib to create an interface? For example, accepting JNA in general with something like:

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

I need to maintain a DLL somewhere: how do I link a DLL with a Jar? I am using Maven to create a Jar file ... but the Native.loadLibrary interface does not allow a direct path.

+4
source share
2 answers

JNI coding is usually a manual process for writing C ++ code to create custom glue methods. There is a whole book that explains this.

In some cases, http://jna.java.net/ can automate or speed up this process, but not count on it.

You cannot โ€œlink native librariesโ€ unless you follow the path of using OSGi or something like the Tanukisoft packaging tool; there is no built-in function in Java for this purpose.

You connect the dots with -Djava.library.path to tell java where to look for your own libraries, or use the lower-level APIs in System.loadLibrary that allow you to specify the full path.

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

+2
source

With BridJ, you can compile the DLL / .so / .dylib just with the JAR, but you must put it (or them) on a specific platform-specific path in the JAR, which starts with "org / bridj / lib /" and ends with the platform architecture identifier +.

Here's a native BridJ source tree that shows this native linking scheme: org / bridj / lib directory

If you adhere to this convention, you will not be dealing with PATH, LD_LIBRARY_PATH or file extraction: BridJ.register() (called by @Library -nominated class with its own methods) will do it all for you!

0
source

All Articles