The library in question is Tokyo Cabinet.
I want is to have the native library, JNI library, and all Java API classes in one JAR file to avoid redistribution headaches.
There seems to be an attempt at this at GitHub, but
- It does not include the actual native library, only JNI library.
- It seems to be specific to Leiningen's native dependencies plugin (it won't work as a redistributable).
The question is, can I bundle everything in one JAR and redistribute it? If yes, how?
P.S.: Yes, I realize it may have portability implications.
You will probably have to unjar the native library to the local file system. As far as I know the bit of code that does the native loading looks at the file system.
This code should help get you started (I haven't looked at it in a while, and it is for a different purpose but should do the trick, and I am pretty busy at the moment, but if you have questions just leave a comment and I'll answer as soon as I can).
JarClassLoader is a class loader to load classes, native libraries and resources from a single monster JAR and from JARs inside the monster JAR.
1) Include the native library into your JAR as a Resource. E. g. with Maven or Gradle, and the standard project layout, put the native library into
main/resources
directory.2) Somewhere in static initializers of Java classes, related to this library, put the code like the following:
Take a look at One-JAR. It will wrap your application up in a single jar file with a specialised class loader which handles "jars within jars" among other things.
It handles native (JNI) libraries by unpacking them to a temporary working folder as required.
(Disclaimer: I've never used One-JAR, haven't needed to as yet, just had it bookmarked for a rainy day.)
https://www.adamheinrich.com/blog/2012/12/how-to-load-native-jni-library-from-jar/
is great article, which solves my issue ..
In my case I've got the following code for initialize the library: