Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packaging JNI Libraries [duplicate]

I am using Java to develop an application and I have a library that I need to be able to use. The library has a jar file and a dll file. At the moment I have placed the dll file into my system folder and linked the jar with the IDE. My question is, I want to package everything into a single executable jar in which the user can run (perhaps using webstart).

How can generate this jar file without having the user copy the dll inside the system folder? Is there a way I can put everything in a single archive?

like image 781
Dave Avatar asked Sep 27 '10 09:09

Dave


2 Answers

You can package the .dll into the .jar, but the physical .dll file must be unpackaged from there before it can be loaded because of how Windows handles .dlls. There's no way to load a .dll directly from inside the .jar.

This question seems to contain an appropriate implementation. Note, though, that it's somewhat error-prone: if the program hasn't write permissions to wherever it's going to extract the .dll,you're in a bizarre situation; you can't load the .dll because you don't have permissions to write it first.

A more robust solution is to use some kind of installer to install the whole application into a directory hierarchy such as

/whatEver/myApp/                 (The working directory of the app.)
/whatEver/myApp/myApp.jar        (The main app.)
/whatEver/myApp/lib/library.dll  (The JNI library.)

Then you can load the dll simply using a path relative to the working directory,

System.loadLibrary("lib/library.dll");

To be even more robust, you'll want to ensure that you're running a JVM with appropriate amount of bits before attempting to load the library. A little-advertised fact is that a 64-bit VM can't load 32-bit libraries, nor vice versa. See this question.

like image 88
Joonas Pulakka Avatar answered Sep 22 '22 12:09

Joonas Pulakka


You might want to have a look at this: http://one-jar.sourceforge.net/ Since you did not provide any information about your build process, I can't help more.

like image 45
Cephalopod Avatar answered Sep 24 '22 12:09

Cephalopod