How to link to .dll from a .jar file

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

Should there be a .dll anywhere? I suggested that, as in the system path, it will be found.

Thanks in advance

Arcs

+4
source share
4 answers

The java.library.path solution is not always good: there are many situations where you cannot change JVM parameters. The best solutions:

1) already mentioned: put the DLL in the same directory as the JAR, unfortunately, this makes using the JAR more difficult - now the JAR is not just a JAR, but has an accompanying DLL

2) put the DLL in the JAR as a regular resource, during the launch of the JAR, extract this DLL somewhere, for example. $ Tmp and then use System.load (new file (...)) as above. Then this JAR is just a JAR, users of this JAR may not even know that it uses any DLL

You can also use the Maven NAR plugin, which is powerful enough if you use Maven to build. See http://duns.github.com/maven-nar-plugin/

+3
source

Try using:

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

System.load(dll.getAbsolutPath());

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

+1
source

I would suggest placing it in the same folder as your application. Having said that β€œit cannot run the DLL file” is a bit vague ... are you sure that this is not a problem with another DLL being a different version (and therefore not loading properly and not found in all)?

In addition, the library search path is determined by the java.library.path parameter .

0
source

Assuming you are using the Sun JVM, you can specify the location of your own libraries on the command line by setting the java library path on the command line.

This can be done using the option "-Djava.library.path = dll directory"

This may depend on how the library is loaded into the java source code. Do you have access to this? Can you post the code?

If the java code uses System.load (String), it expects the full path to the dll.

If it uses System.loadLibrary (String), it expects only the library name and will be in the location specified by the java.library.path parameter.

0
source

Source: https://habr.com/ru/post/1305563/


All Articles