Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a .dll from a .jar file

Tags:

java

jar

dll

I have an application which uses methods in a .jar which calls a .dll. This works fine for me on my machine (when the app is unpackaged or run as a .jar itself) but when the application.jar is run on another machine, with the external .dll on the system path, it cannot run the dll file.

Does the .dll have to be located anywhere special? I has assumed that as it was on the system path that it would be found.

Thanks in advance

Dougie

like image 427
Dougie Avatar asked Mar 30 '10 15:03

Dougie


2 Answers

java.library.path solution isn't always good: there are many situations where you can't change JVM parameters. Better solutions:

1) already mentioned: put DLL in the same directory as JAR, unfortunately it makes usage of such a JAR harder - now the JAR isn't just a JAR, but has an accompanying DLL

2) put the DLL into the JAR as a normal resource, during JAR startup extract this DLL somewhere to e.g. $TMP, and then use System.load(new File(...)) as above. Then this JAR is just a JAR, users of this JAR may even don't know that it uses any DLLs

You can also use Maven NAR plugin, which is quite powerful if you're usin Maven for builds. See http://duns.github.com/maven-nar-plugin/

like image 83
iirekm Avatar answered Sep 20 '22 02:09

iirekm


Try to use:

File dll = new File([...]);

System.load(dll.getAbsolutPath());

I would package the dll in the same directory as your jar-archive.

like image 28
Christian Kuetbach Avatar answered Sep 19 '22 02:09

Christian Kuetbach