0

Why does same jar contents with name shaded.jar fail to load dll while with name sigar.jar , dlls load successfully?

I am using shaded jar in maven.It creates a jar: shaded.jar.

I have an artifact :

<dependency>

        <groupId>org.hyperic</groupId>

        <artifactId>sigar</artifactId>

        <version>1.6.5.132-7</version>

    </dependency

I use it in my code like:

SigarProxyCache.newInstance(new Sigar(), 100).getPid();

Since this jar uses dll, I have kept all the dlls in the same folder as shaded.jar, but still i get following exception:

Caused by: org.hyperic.sigar.SigarException: java.lang.UnsatisfiedLinkError: org.hyperic.sigar.Sigar.getPid()J at org.hyperic.sigar.SigarProxyCache.invoke(SigarProxyCache.java:220) ... 2 more

If I add sigar.jar (same version that has been added to shaded.jar) to classpath directly instead of packaging it in shaded .jar, no exception is thrown and dll is loaded successfully. (Since jar and lib are in same folder, there wasn't any need to modify the java.library.path)

Furthermore, even if I remove sigar.jar from classpath and rename my shaded.jar(this one contains sigar packaged) to sigar.jar, dll loading is still successful.

Now I have referred following posts:

Extract and load DLL from JAR

UnsatisfiedLinkError: no opencv_java249 in java.library.path

which suggest a solution but my question is, why does changing the name impact dll loading. Since inside shaded.jar as well ass sigar.jar, the package structure is exactly same and in both cases dlls are lying parallel to jar.

Here is list of dll/so files that sigar.jar needs, they all contain sigar in their name: enter image description here

tryingToLearn
  • 10,691
  • 12
  • 80
  • 114
  • Maybe someone thought it would be clever to derive the DLL name from the name of the jar – user2543253 Jul 10 '18 at 14:22
  • Are you guessing or this is how it works? – tryingToLearn Jul 10 '18 at 14:24
  • Guessing, of course. Because that's not how it usually works. Also putting the lib in the same directory as the jar doesn't usually make a difference. If it does for you, that means someone has written extra code to find the lib. You could try renaming the lib too – user2543253 Jul 10 '18 at 14:46
  • Yes, I tried renaming the lib by replacing 'sigar' with 'shaded', it doesn't work though – tryingToLearn Jul 10 '18 at 15:12

1 Answers1

2

Was curious. Had a look at the code at github. To find the name of the native library they take the class name, search the classpath for a jar file with the same name then look in the directory where it's found. When you put the classes in a file with another name, the jar isn't found, so the same-directory approach fails.

user2543253
  • 2,143
  • 19
  • 20