This my second time coding Java and never referencing any external library before. I follow the JNI examples online and I get UnsatisfiedLinkError when trying to load the dll. I thought that I have to create DLL first before trying to load, but all the examples I’ve looked they don’t mention about creating DLL. Most of them stating that I should create Java code first then native code.
public class ClassSample1
{
public native void displayHelloWorld();
static
{
System.loadLibrary("MyLittleJNI");
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
ClassSample1 classSample1;
classSample1 = new ClassSample1();
classSample1.displayHelloWorld();
System.out.println("Hello");
}
}
How can I get ride of the error?
The sample code you provide assumes that there is a DLL in the search path called
MyLittleJNI.dll, containing a methoddisplayHelloWorld. The actual C function name in the DLL is decorated using a well defined syntax.If you get an
UnsatisfiedLinkErrorin loadLibrary(), it is because the JVM cannot find the DLL. You can duck the issue temporarily by specifying the full pathname to the DLL using theSystem.load(filename)method instead.Once
loadorloadLibrarysucceeds, you need to make sure that the native function is named correctly. To aid in this, you can usejavahto generate a header file containing prototypes for all the native functions in a class.More information about how to use JNI can be found in here and here.
EDIT: Also, the “Related” column to the right of this questions seems to contain several useful related question.