简体   繁体   中英

Loading DLLs with JNA on Glassfish

I'm having issues deploying a servlet. The servlet uses JNA to load some DLLs, which are placed inside the classpath com.myproject..etc. (eg "my_dll.dll"):

public static final MyDllInterface INSTANCE = (MyDllInterface) Native.loadLibrary("./com/myproject/main/jna/dll/my_dll", MyDllInterface.class);

This works fine as long as I test these JNA Interfaces locally with test classes. As soon as I deploy them to glassfish (via Netbeans) I get errors like this:

java.lang.UnsatisfiedLinkError: Unable to load library './com/myproject/main/jna/dll/my_dll': The specified module could not be found.

I am suspecting it has something to do with how glassfish handles classpaths or maybe how loading inside *.war packed application works.

Things I've tried using:

  • relative class paths (../dll_file)
  • absolute class paths (com/etc and /com/etc)
  • absolute system paths (C:/Users/etc)

Can anyone help?

(I'm using Netbeans 8.2, Glassfish 4.1.1 (comes with Netbeans) on Windows. DLLs were compiled on Windows C++ and work with my testing code)

I guess the problem is related to java.library.path and that you are packaging the DLL files inside your webapp. This is not recommended.

About the variable java.library.path :

When a Java application loads a native library using the System.loadLibrary() method, the java.library.path is scanned for the specified library. If the JVM is not able to detect the requested library, it throws an UnsatisfiedLinkError. Finally, the usage of native libraries makes a Java program more platform dependent, as it requires the existence of specific native libraries.

So on Glassfish this variable is normally set to the JVM folders and the glassfish/lib directories.

One solution may be, to put the DLL files into the folder glassfish/lib and remove the paths from your loadLibrary() calls.

If this doesn't work or is not portable enough for you, you can try to set the java.library.path to the path of your exploded WAR directory (ths is the directory where the content of your WAR file is extracted when it is deployed on Glassfish).

You can change the variable for your Glassfish domain via the Glassfish Admin UI, with the asadmin utility or directly in your domain.xml .

Here is an example: -Djava.library.path=c:\\somewhere\\where\\the\\DLLs\\are\\located

The path to your exploded web application should look similar to this: glassfish\\domains\\domain1\\applications\\YOUR_WEBAPPLICATION\\WEB-INF\\classes\\whereTheDLLsAre\\ . Have a look at this folder to make sure, the DLL files are there.

Then your setting should look similar to this:

-Djava.library.path=c:\\SOMETHIN\\glassfish4\\glassfish\\domains\\domain1\\applications\\YOUR_WEBAPPLICATION\\WEB-INF\\classes\\whereTheDLLsAre\\

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM