错误:JnitiveMethod nfptr引发从jobjectarray*到void的无效转换*

f0brbegy  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(250)

我有以下简单的jni应用程序:

jobjectArray ListItems(JNIEnv *env, jobject self, jstring library)
{
   jclass c = env->FindClass("java/lang/String");
   jobjectArray a = env->NewObjectArray(2, c, 0);
   env->SetObjectArrayElement(a, 0, env->NewStringUTF("text1"));
   env->SetObjectArrayElement(a, 1, env->NewStringUTF("text2"));
   return a;
}

int main()
{
      ...
      ...*load and initialize Java VM and JNI interface*
      ...
      JNINativeMethod m[1];
      m[0].fnPtr = ListItems;
      m[0].name = "listItems";
      m[0].signature = "(Ljava/lang/String;)[Ljava/lang/String;";
      env->RegisterNatives(myJavaClass, m, 1);
}

当我编译如下代码时,它会告诉我“错误:从jobjectarray到void的转换无效”

g++ -I"JNI\include" -I"JNI\include\win32" test.cpp -o test.exe
yhuiod9q

yhuiod9q1#

正如@michael所说,只需要一个空*铸造:

int main()
  {
      ...
      ...*load and initialize Java VM and JNI interface*
      ...
      JNINativeMethod m[1];
      m[0].fnPtr =(void*) ListItems;
      m[0].name = "listItems";
      m[0].signature = "(Ljava/lang/String;)[Ljava/lang/String;";
      env->RegisterNatives(myJavaClass, m, 1
  }

相关问题