views:

200

answers:

2

Hello all,

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

+2  A: 

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.

Bogdan
+1  A: 

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 :)

Chris Dennett
Thank you - I made it work by including the library as a resource and extracting it at runtime. It can then be loaded with System.load(). Picked this as the correct answer because it's more detailed - will check out One-Jar, though.
Martin Wiboe