Loading a dll from the jar file or from the jar folder

3,689 views
Skip to first unread message

Alessandro Vincenzi

unread,
Apr 8, 2015, 5:59:28 AM4/8/15
to jna-...@googlegroups.com
Hi

I am using JNA to wrap an old API "Library.dll" and get the equivalent API as a new "Library.jar".

I cannot find a way to avoid setting the jna.library.path or putting the ddl in the folder pointed by the system PATH variable. This because the Library.jar is going to be delivered to customers.

How could I (if possible)

1 - include the dll in the Library.jar and ask jna to load it from there (this way we distribute just the jar file)

or

2 - to ask jna to load Library.dll from the same folder where the Library.jar is (this way we distribute both libraries but placed in the same folder)?

Thank You

Alessandro

Timothy Wall

unread,
Apr 9, 2015, 8:45:09 AM4/9/15
to jna-...@googlegroups.com
JNA provides a function (Native.extractFromResourcePath) to extract “Library.dll” from the class path, returning a File object pointing to the extracted file. You can then move this or rename it and use its absolute path when calling Native.loadLibrary() or Native.register().

You’d typically only want to extract it once on first run/install.
> --
> You received this message because you are subscribed to the Google Groups "Java Native Access" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

AV

unread,
Apr 9, 2015, 10:05:35 AM4/9/15
to jna-...@googlegroups.com
Thank you for your answer.

I am sorry but I don't see how using Native.extractFromResourcePath(String name) solves my problem.

I need to load a Library.dll and its n dlls dependencies as well. These 1+n dlls can be either inside a Library.jar or on the same folder as Library.jar  The problem is that we don't deliver the jar with an installer therefore i don't know where our users will save it on his disk.

So,

- Assuming that my dlls are inside the jar and that I know where Library.jar is, is there a way to load the main Library.dll and its dependencies as well?

- Assuming that my dlls are in the same folder as the jar, how can I get the absolute path to that folder so I can set the variable "jna.library.path" before calling the Native.loadLibrary()?

Can you please write down an example?

Alessandro

Timothy Wall

unread,
Apr 10, 2015, 4:33:04 AM4/10/15
to jna-...@googlegroups.com
// Figure out current location of Library.jar
// Extract all DLLs to that location if not already
// Set jna.library.path to include the current location of Library.jar and DLLs
File currentDir = myGetLibraryJarPath(System.getProperty(“java.class.path”));
if (!installed) {
for (dll : all_dlls) {
File file = Native.extractFromResourcePath(dll);
file.move(currentDir);
}
installed = true;
}
System.setProperty(“jna.library.path”, currentDir);

Note that this assumes the process has write access, but you can make that a requirement of first run of the program to ensure that this is set up properly.
Reply all
Reply to author
Forward
0 new messages