Tuesday, September 6, 2011

Exception in thread "main" java.lang.UnsatisfiedLinkError: Could not load library

Exception in thread "main" java.lang.UnsatisfiedLinkError: 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)



Scenario
The above is the error sometimes we get, when we try to load a DLL built using Microsoft Visual Studio. This error commonly happens when, one try to load a DLL built on his/her PC and distribute to others. In such situation, the library will be successfully loaded in the source machine in which the DLL is built, but will fail to get loaded in other machines causing the above error. This is due to the library dependencies that gets created when building DLL.

Reason
        This is due to the library dependencies as said above. Microsoft Visual Studio is highly sensitive on the Build configuration, due to which the generated DLL's dependencies may get affected. For example, the "sample.dll" built may have dependencies on some "XYZ.dll version a.b.c". This "XYZ.dll version a.b.c" may exist in multiple version on a machine due to various reasons, such as Visual Studio version installed, Internet explorer installed etc. Its responsibility of the System, or Application to find the dependencies correct version and load the required DLL. This responsibility factor is decided based on the RT_MANIFEST present in DLL generated.

Solution
Sol -1.There are various tools available for checking the dll dependencies. "Dependency Walker" is one of the common tool used to check such dependencies. Once you find the dll dependencies, try to copy these missing dll's in respective folders based on the dll category such as (System dll, application dll's). If all the dll's exist go for the next solution.

Sol -2. Build the DLL using "release" mode instead of "Debug". This avoids the Visual C++ Redistribution package DLL dependencies being getting added in the RT_MANIFEST. If still unable to load the library in java, go for the final solution below.

Sol -3. The Project Properties->Code Generation -> Runtime Library option play a crucial role in dll creation. Based on this many dependencies gets added or removed such as MSVCR80D.DLL, MSVCP80D.DLL, IESHIMS.DLL. One solution would be build the DLL as Multi threaded instead of Multi thread DLL as shown

No comments:

Post a Comment