Thursday, November 17, 2011

UnsatisfiedLinkError Hell

I believe in the last few hours I have been through all possible variations of the dreaded UnsatisfiedLinkError. I build a native library and wanted to integrate it using JNI.

java.lang.UnsatisfiedLinkError: no xxx in java.library.path

This is an easy one. Just set the path of the library in the java.library.path system property and don't add a „lib“ prefix or „.dll“ / „.so“ suffix in the System.loadLibrary call.

java.lang.UnsatisfiedLinkError: xxx.dll: Can't find dependent libraries

This one is a bit trickier. Unfortunately Java doesn't tell what is missing. On Windows use a tool like Dependency Walker to identify what is missing. I also had to load all the dependent libraries with System.loadLibrary calls (in correct order) before loading my own library.

You're likely to hit another problem if you're on Window and build a DLL with Eclipse CDT / Cygwin / minGW. If your code tries to call a native method you get yet another UnsatisfiedLinkError with the name of the native method as the error message. It means that this JNI function was not found in the native library. You have to use the GCC linker flag „-Wl,--kill-at“ to build the DLL in a JNI conform way.

On Linux make sure that you have compiled the library with the -fPIC flag.

Hope that helps someone.


pirateCat said...

Thank you so much! That helped a lot. -Wl,--kill-at ... of course?! Who would think it could be something different!

Mark Gallaher said...

Hi, if you need a dll library, here is one of the best You just need to download the file you need and add it to the system.