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: