I am running Windows XP. Obviously, JNI and UnsatisfiedLinkError go hand in hand ... I noticed that most of the time the linker error looks like this:
java.lang.UnsatisfiedLinkError: no whatever.dll in java.library.path
But that is not my problem; Java can find my dll. I get an error that makes me think that I called my method wrong:
java.lang.UnsatisfiedLinkError: NativeTest.nativemethod(lJava/lang/String;)Z
I tried to address a number of similar issues in StackOverflow, for example this , this , this , this , and this , but none of these methods worked. I also found this thread on the Ubuntu forums where it looks like the exact same problem as mine, but the question didn’t answer how they fixed their own problem (which really sucks). All google searches gave me an error just like java.library.path.
Here is my actual code.
NativeTest.java
class NativeTest
{
public static native boolean nativemethod (String arg);
public static void main (String[] args)
{
System.out.println(nativemethod("0123456789"));
System.out.println(nativemethod("012"));
}
static { System.loadLibrary("NativeTest"); }
}
NativeTest.h
#include <jni.h>
#ifndef _Included_NativeTest
#define _Included_NativeTest
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jboolean JNICALL Java_NativeTest_nativemethod
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
}
#endif
#endif
NativeTest.c
#include <jni.h>
#include <windows.h>
#include "NativeTest.h"
JNIEXPORT jboolean JNICALL Java_NativeTest_nativemethod
(JNIEnv* Jenv, jclass Jref, jstring Jarg)
{
MessageBox(NULL, "text", "title", MB_OK);
int len = (*Jenv)->GetStringLength(Jenv, Jarg);
return (jboolean)(len > 5);
}
In cmd.exe:
(The gcc command is my mishmash of various commands that I found on the Internet.)
>javac NativeTest.java
>javah -jni NativeTest
>gcc -shared -I<jdk_dir>\include -I<jdk_dir>\include\win32 -oNativeTest.dll NativeTest.c -lgdi32
>java -Djava.library.path=. NativeTest
Exception on thread "main" java.lang.UnsatisfiedLinkError: NativeTest.nativemethod(Ljava/lang/String;)Z
at NativeTest.nativemethod(Native Method)
at NativeTest.main(NativeTest.java:8)
>java NativeTest
Exception on thread "main" java.lang.UnsatisfiedLinkError: NativeTest.nativemethod(Ljava/lang/String;)Z
at NativeTest.nativemethod(Native Method)
at NativeTest.main(NativeTest.java:8)