RegisterNatives: NoSuchMethodFound
I'm just starting to learn JNI at the moment. I had this working fine just using javah to create a header file, but I'm trying to get away from that and use the RegisterNatives method from the JNI_OnLoad method.
However, I've been banging away at errors for a while, but now I'm stuck. Any ideas?
C++:
#include"jni.h"
JNIEXPORTvoid JNICALL native_hello (JNIEnv *env, jobject obj, jstring s)
{
printf("Hello, ");
constchar *str = env->GetStringUTFChars(s, 0);
printf(str);
env->ReleaseStringUTFChars(s, str);
printf("!\n");
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
//set up the java method
JNINativeMethod nativeMethod;
nativeMethod.name="hello";
nativeMethod.signature="(java/lang/String;)";
nativeMethod.fnPtr=*native_hello;
//set java environment
JNIEnv *env;
vm->GetEnv((void**)&env, JNI_VERSION_1_4);
jclass clazz=env->FindClass("JNI1/Test1");
env->RegisterNatives(clazz, &nativeMethod, 1);
return JNI_VERSION_1_4;
}
Java:
package JNI1;
publicclass Test1{
static
{
try{
System.loadLibrary("TestDll");
}
catch(NoSuchMethodError e)
{
e.printStackTrace();
}
}
publicstaticvoid main(String ar[])
{
/*Test1 t=new Test1();
t.hello("Fred"); */
}
publicnativevoid hello(String name);
}
And I get this error:
java.lang.NoSuchMethodError: hello
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1751)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1676)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:992)
at JNI1.Test1.<clinit>(Test1.java:7)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:87)

