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 .dll
s. 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.