Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include platform-specific native libraries in the .JAR file using Eclipse?

I am just starting to learn JNI. I have been following a simple example, and I have created a Java app that calls a Hello World method in a native library. I'd like to target Win32 and Linux x86.

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

So my question is basically; how would you go about creating a project in Eclipse and include several versions of the same native library?

Thank you,
Martin

like image 874
Martin Wiboe Avatar asked Apr 23 '10 21:04

Martin Wiboe


Video Answer


2 Answers

For runnable JARs, what you need to do is extract to the temporary directory (maybe 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 to allow libraries to be found in that directory. You can do whatever logic you need to here to load the particular platform libraries. To be honest, the naming for the libraries on the different platforms should be similar anyway -- I believe that you omit the 'lib' part when loading, and the extension. That's the gist of it. Probably easier to use One-JAR as the other poster mentioned :)

like image 156
Chris Dennett Avatar answered Oct 11 '22 15:10

Chris Dennett


You might want to check out the One-JAR project. It lets you package your application and its dependencies (including native libraries) to a single jar file.

like image 24
Bogdan Avatar answered Oct 11 '22 17:10

Bogdan