Windows XP を実行しています。どうやら JNI と UnsatisfiedLinkError は密接に関係しているようです... ほとんどの場合、リンカー エラーは次のようになっていることに気付きました。
java.lang.UnsatisfiedLinkError: no whatever.dll in java.library.path
しかし、それは私の問題ではありません。Java は私の DLL を見つけることができます。メソッドの名前が間違っていると思わせるエラーが表示されます。
java.lang.UnsatisfiedLinkError: NativeTest.nativemethod(lJava/lang/String;)Z
this one、this one、this one、this one、およびthis oneのような StackOverflow に関する同様の質問をいくつか調べてみましたが、これらの方法はどれも機能しませんでした。私が抱えているのとまったく同じ問題のように見えるUbuntuフォーラムでもこのスレッドを見つけましたが、質問者は自分の問題をどのように修正したかを述べていませんでした(これは本当にひどいです)。これに関するすべてのGoogle検索で、 java.library.path と同じエラーが表示されました。
これが私の実際のコードです。
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
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class NativeTest */
#ifndef _Included_NativeTest
#define _Included_NativeTest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: NativeTest
* Method: nativemethod
* Signature: (Ljava/lang/String;)Z
*/
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);
}
cmd.exe の場合: (gcc コマンドは、インターネットで見つけたさまざまなコマンドの寄せ集めです。)
>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)