I have an application which uses methods in a .jar which calls a .dll. This works fine for m开发者_运维问答e 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
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/
Try to use:
File dll = new File([...]);
System.load(dll.getAbsolutPath());
I would package the dll in the same directory as your jar-archive.
I would suggest putting it in the same folder as your application. Having said that, "it cannot run the dll file" is a little vague... are you sure that it isn't an issue with the other DLL being a different version (and therefore not loading properly, rather than not being found at all)?
Also, the library search path is defined by the java.library.path parameter.
Assuling you are using the Sun JVM the location of native libraries can be specified on the command line by setting the java library path on the command line.
This can be done with the option "-Djava.library.path=dll directory"
It can depend on how the library is loaded in the java source code. Do you have access to this? Can you post the code?
If the java code uses System.load(String) it will expect the full path to the dll.
If it uses System.loadLibrary(String) it expects only the library name and will seach in the location specified by the java.library.path parameter.
精彩评论