title: Getting an UnsatisfiedLinkError (undefined symbol) in Java while loading JNI dependencies even after successfully loading the required object file
body: <p>I'm using Google OR-tools library (v6.4) for a project (though my question is not specific to this library). This consists of one jar, which has a few native dependencies (a bunch of ".so"/".dylib" object files, depending on the OS). This build for my project is being made on Ubuntu 14.04</p>
  
  <p>The problem I'm facing: On trying to load a specific object file at runtime (using <code>System.load()</code>), I'm getting an UnsatisfiedLinkError with the message as "undefined symbol" (I've added the stacktrace below). However, I am loading the object file defining this symbol just before this, so I'm not sure why this error is being thrown. </p>
  
  <p>I'm loading the dependencies in the following way: The object files are being packed into the jar created by Maven during build, and are being extracted and loaded (using <code>System.load()</code>) at runtime. The method for that is as follows:</p>
  
  <pre><code>public class EnvironmentUtils {
  
      public static void loadResourceFromJar(String prefix, String suffix) {
          String tempFilesDirectory = System.getProperty("java.io.tmpdir");
          File tempFile = null;
          try {
              tempFile = new File(tempFilesDirectory + "/" + prefix + suffix);
              tempFile.deleteOnExit();
              try (final InputStream inputStream = EnvironmentUtils.class.getClassLoader().
                      getResourceAsStream(prefix+suffix)) {
                  if (inputStream == null) {
                      throw new RuntimeException(prefix + suffix + " was not found inside JAR.");
                  } else {
                      Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                  }
              }
              System.load(tempFile.getAbsolutePath());
          } catch (Exception e) {
              //Log top 10 lines of stack trace
          }
      }
  }
  </code></pre>
  
  <p>This method is being called inside a static block for all dependencies:</p>
  
  <pre><code>public class DummyClass {
      static {
          String sharedLibraryExtension = EnvironmentUtils.getSharedLibraryExtension(); //.so for linux, .dylib for Mac
          String jniLibraryExtension = EnvironmentUtils.getJniLibraryExtension(); //.so for linux, .jnilib for Mac
          EnvironmentUtils.loadResourceFromJar("libfap", sharedLibraryExtension);
          EnvironmentUtils.loadResourceFromJar("libcvrptw_lib", sharedLibraryExtension);
          EnvironmentUtils.loadResourceFromJar("libortools", sharedLibraryExtension);
          EnvironmentUtils.loadResourceFromJar("libdimacs", sharedLibraryExtension);
          EnvironmentUtils.loadResourceFromJar("libjniortools", jniLibraryExtension);
      }
  }
  </code></pre>
  
  <p>On running <code>System.load()</code> for libdimacs.so, an UnsatisfiedLinkError is thrown. Stacktrace:</p>
  
  <pre><code>java.lang.UnsatisfiedLinkError: /tmp/libdimacs.so: /tmp/libdimacs.so: undefined symbol: _ZN6google14FlagRegistererC1IbEEPKcS3_S3_PT_S5_
      at java.lang.ClassLoader$NativeLibrary.load(Native Method)
      at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
      at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
      at java.lang.Runtime.load0(Runtime.java:809)
      at java.lang.System.load(System.java:1086)
      at com.(PROJECT_NAME).utils.EnvironmentUtils.loadResourceFromJar(EnvironmentUtils.java:78)
      at com.(PROJECT_NAME).DummyClass.&lt;clinit&gt;(DummyClass.java:28)
  </code></pre>
  
  <p>However, this symbol "_ZN6google14FlagRegistererC1IbEEPKcS3_S3_PT_S5_" is present in libortools.so, which is being loaded before libdimacs. I verified this by running the following command:</p>
  
  <pre><code>objdump -t (LIBRARY_PATH)/libortools.so | grep _ZN6google14FlagRegistererC1IbEEPKcS3_S3_PT_S5_
  </code></pre>
  
  <p>This gave me the following output:</p>
  
  <pre><code>0000000000ce12cc gw    F .text       00000091 _ZN6google14FlagRegistererC1IbEEPKcS3_S3_PT_S5_
  </code></pre>
  
  <p>So it would seem that the symbol should have been defined at the time of the <code>System.load()</code> call, unless there was some issue in loading the containing object file. To check if the object file had been loaded correctly, I used the approach detailed in <a href="https://stackoverflow.com/questions/1007861/how-do-i-get-a-list-of-jni-libraries-which-are-loaded/1008631#1008631">this solution</a>. Apart from the class detailed in that answer, I added the following lines after <code>System.load()</code> call in <code>EnvironmentUtils.loadResourceFromJar()</code> to print the most recently loaded library name:</p>
  
  <pre><code>public class EnvironmentUtils {
  
      public static void loadResourceFromJar(String prefix, String suffix) {
          ...
          System.load(tempFile.getAbsolutePath());
          final String[] libraries = ClassScope.getLoadedLibraries(ClassLoader.getSystemClassLoader());
          System.out.println(libraries[libraries.length - 1]);
      }
  }
  </code></pre>
  
  <p>The output (till just before the UnsatisfiedLinkError) is as follows:</p>
  
  <pre><code>/tmp/libfap.so
  /tmp/libcvrptw_lib.so
  /tmp/libortools.so
  </code></pre>
  
  <p>So libortools.so seems to be loading correctly, which means the symbol should be loaded in memory. The exact same code is working perfectly with the corresponding Mac (".dylib") dependencies (Built on MacOS Sierra 10.12.5). Would appreciate any advice on resolving this. Thank you.</p>