Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8222096

System.loadLibrary() can't find dependent libraries, raises UnsatisfiedLinkError

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      OS:
      Edition: Windows 10 Enterprise
      Version: 1803
      Installed on: 22/03/2019
      OS build: 17134.648

      System: Virtualbox 5.2.26 r128414 on macOS Mojave 10.14.4

      (however I reproduced the same bug on a different Windows 10 setup on a physical laptop).

      Java: Oracle JDK 1.8.0_202 (but the same bug can be reproduced with Oracle JDK 1.8.0_

      A DESCRIPTION OF THE PROBLEM :
      If System.loadLibrary is used to load a native (.dll) Windows library that depends on another native (.dll) Windows library, the former is loaded but the latter is not. In the reported example that allows to reproduce the issue, a class tries to load a DLL glpk_4_65_java.dll by invoking System.loadLibrary. This .dll, in turns, depends on another DLL glpk_4_65.dll. Notwithstanding the fact that both DLLs are in the same directory and that the java.library.path points to that directory, only the first DLL is loaded. The second DLL is not loaded and an exception is raised.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1- Download the exampleBugSystemLoadLibrary.zip archive at the following link:

      https://www.dropbox.com/s/d85dt113sfwie65/exampleBugSystemLoadLibrary.zip?dl=0

      2- Expand the .zip file under C:\ yielding:
        - a C:\glpk-java.jar file;
        - a C:\glpk-4.65 folder, and
        - a C:\ExampleDLL folder.
      2- Open a Windows console and set the path to point to a JDK 1.8.0_202 tools.
      3- Compile the example program:

          cd c:\ExampleDLL
          javac -cp c:\glpk-java.jar Example.java %for convenience, an Example.class compiled with javac 1.8.0_202 is included

      4- Run the example program:
          java -cp c:\glpk-java.jar;c:\ExampleDLL -Djava.library.path=c:\glpk-4.65\w64 Example



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program terminates without producing any output.
      ACTUAL -
      The program terminates producing the following output:

      Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\glpk-4.65\w64\glpk_4_65_java.dll: Can't find dependent libraries
      at java.lang.ClassLoader$NativeLibrary.load(Native Method)
      at java.lang.ClassLoader.loadLibrary0(Unknown Source)
      at java.lang.ClassLoader.loadLibrary(Unknown Source)
      at java.lang.Runtime.loadLibrary0(Unknown Source)
      at java.lang.System.loadLibrary(Unknown Source)
      at Example.main(Example.java:5)

      ---------- BEGIN SOURCE ----------
      import org.gnu.glpk.GLPKJNI;

      public class Example {
      public static void main(String[] args) throws ClassNotFoundException {
      System.loadLibrary("glpk_4_65_java");
      //Class.forName("org.gnu.glpk.GLPKJNI");
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      If we comment out line 5 of Example.java, uncomment line 6, and recompile/rerun Example.java the program terminates successfully without raising any exception. Line 6 loads a class org.gnu.glpk.GLPKJNI contained in C:\glpk-java.jar. The static initializer of the class invokes System.loadLibrary("glpk_4_65_java"), a statement identical to Example.java:5, but this time the dependency is loaded and the exception is not thrown.

      Note that under a different setting (that I am not currently able to reproduce) the exception is raised also when the static initializer of org.gnu.glpk.GLPKJNI is invoked.


      FREQUENCY : always


            mchung Mandy Chung (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: