code-gijoe
code-gijoe

Reputation: 7244

How to debug loadLibrary to understand why not loading a DLL?

I'm trying to load a DLL with a static block on my servlet definition like this:

    String ehrViewerExternalNativeLibs = "webapps" + pathSeparator +
                                         "ehr-viewer" + pathSeparator +
                                         "WEB-INF" + pathSeparator +
                                         "classes" + pathSeparator +
                                         "extlib";

    try {
        String catalinaHome = System.getProperty("catalina.home");
        String defaultLibraryPath = System.getProperty("java.library.path");
        String sharedLibraryPath = catalinaHome + pathSeparator + ehrViewerExternalNativeLibs;

        if (catalinaHome != null) {
            System.setProperty("java.library.path", defaultLibraryPath + ";" + sharedLibraryPath);
            String curPath = System.getProperty("java.library.path");
            logger.info(curPath);
            System.loadLibrary("awj2k");
            //System.load(sharedLibraryPath + "\\awj2k.dll");
            Class.forName("com.aware.j2k.codec.engine.AwJ2k");
        }

I keep getting an error like this:

Mar 10, 2015 11:17:27 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Allocate exception for servlet ehrViewerServiceImpl

java.lang.UnsatisfiedLinkError: no awj2k in java.library.path at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) at com.softmedical.ehrviewer.server.EHRViewerServiceImpl.(EHRViewerServiceImpl.java:121)

The output for the library path is :

INFO | 2015-03-10 11:17:27,558 | - D:\Tomcat-7.0\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;;.;D:\Tomcat-7.0\webapps\ehr-viewer\WEB-INF\classes\extlib

And the content of folder extlib is

awj2k.dll

If I put the DLL in D:\Tomcat-7.0\bin it works just fine. What dark spell is going behind the curtains? Why can I load a DLL from the place I want? I see the java.library.path is set correctly so why is it not working?

Upvotes: 0

Views: 2374

Answers (1)

Kevin Condon
Kevin Condon

Reputation: 1728

The classloader is probably not using your runtime changes to java.library.path. See Adding new paths for native libraries at runtime in Java.

Can you load the library using System.load in the servlet's init? Something like this.

public void init(ServletConfig config) {
  ServletContext ctx = config.getServletContext();
  String libPath = ctx.getRealPath("/") + "\WEB-INF\classes\extlib\awj2k.dll";
  System.load(libPath);
}

EDIT: I just realized you asked for a debugging approach. Try defining java.library.path to include the dir with awj2k.dll in the Tomcat startup config or on the command line. If System.loadLibrary works, that would confirm that library path changes at runtime are not effective.

Upvotes: 1

Related Questions