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?
You can package the
.dllinto the.jar, but the physical.dllfile must be unpackaged from there before it can be loaded because of how Windows handles.dlls. There’s no way to load a.dlldirectly 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.dllbecause 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
Then you can load the dll simply using a path relative to the working directory,
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.