How can I include my own platform-specific libraries in a .JAR file using Eclipse?

I'm just starting to learn JNI. I followed a simple example, and I created a Java application that calls the Hello World method in a native library. I would like to target Win32 and Linux x86.

My library is in a DLL, and I can name it just fine using LoadLibrary when a DLL is added to the root of my Eclipse project.
However, I cannot figure out how to get Eclipse to export an executable JAR that includes a DLL and a .SO file for Linux.

So my question is basically; how would you like to create a project in Eclipse and include multiple versions of the same native library?

Thanks,
Martin

+6
java linux windows cross-platform jni
source share
2 answers

For executable JAR files, you need to extract to a temporary directory (possibly in a static block {}), and then load the DLL from that directory (using System.loadLibrary () in the same block). To do this, you need to subclass ClassLoader and override the findLibrary () method so that libraries can be found in this directory. You can do all the logic needed to load certain platform libraries. To be honest, the naming of libraries on different platforms should be the same anyway - I believe that you omit the "lib" part at boot time and the extension. This is the essence of this. It's probably easier to use One-JAR, as another poster mentioned :)

+4
source share

You might want to check out the One-JAR project. It allows you to pack your application and its dependencies (including its own libraries) into a single jar file.

+3
source share

All Articles