Friday, November 7, 2014

JNI/Win32:java.lang.UnsatisfiedLinkError

I spent almost two whole days trying to solve this issue. I did everything possible from altering my c++ code (changing the return types) to compiling from command line rather than the IDE. Also, I tried a number of other steps like changing the classpaths, refactoring package names to changing names of the generated header files. Finally, I found the following resolution.

Whenever the C++ compiler (GCC) generates a DLL, it is exported in the following form:
Java_SomePackage_SomeClass_SomeNativeMethod@8

Where the integer suffix suggests the byte space required by the arguments. This sort of function call makes no sense to the JVM (while invoking someNativeMethod) and hence it leads to java.lang.UnsatisfiedLinkError. The resolution is to add the following flags while linking to generate unmangled names:
-Wl,--add-stdcall-alias

This will create an alias name (pure method call name) to the generated method. This allows the JVM to invoke the right method via JNI.

I almost gave up on this issue, before I finally found this resolution. The point to note here is that this happened to me only on Win32 - I had no issues running on the UNIX platform.

No comments: