java - jni getMethodID 用于获取构造函数 ID 时返回 Null

标签 java java-native-interface jnienv jniwrapper

I have assigned jvm to global variable in a function using

int status = (*jenv)->GetJavaVM(jenv, &jvm);
if(status != 0) {
    printf(" Fail!\n");
         }

classNameC是一个全局变量,它的类没有构造函数。然后在其他函数中,我这样使用它:

JNIEnv *env;
printf("starting function\n");
(*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
printf("thread attached\n");
jclass local = (*env)->FindClass(env,classNameC);
if(local!=NULL)
{
printf("1st class found\n");
}
jmethodID constructor=(*env)->GetMethodID(env,local,"<init>","()V");
if(constructor==NULL)
{
printf("1st Constructoris NULL\n");
}
else
{
printf("1st Constructor created\n");
}
jobject classObject=(*env)->NewObject(env,local,constructor);
if(classObject==NULL)
{
printf("1st object is  NULL\n");
}
else
{
printf("1st object is  created\n");
}

jclass local1 = (*env)->FindClass(env,"SWIGTYPE_p_void");
if(local1==NULL)
{
printf("SWIGTYPE p void class is NULL\n");
}
else
{
printf("SWIGTYPE p void class created\n");
}

这个构造函数有 2 个参数,第一个是长整型,第二个是 boolean 型。该类有 2 个构造函数,其中一个没有参数,并且用 0 初始化成员。

jmethodID constructor1=(*env)->GetMethodID(env,local1,"<init>","(J;Z;)V");
if(constructor1==NULL)
{
printf("SWIGTYPE p void constructor is NULL\n");
}
else
{
printf("SWIGTYPE p void constructor is created\n");
}

当我运行它时,它成功打印到创建了 SWIGTYPE p void 类,并且 SWIGTYPE p void 构造函数为 NULL,然后给出了此错误:

A fatal error has been detected by the Java Runtime Environment:

SIGSEGV (0xb) at pc=0x00007f7ec503fa7b, pid=25307, tid=140182441326336

JRE version: Java(TM) SE Runtime Environment (7.0_65-b17) (build 1.7.0_65-b17) Java VM: Java HotSpot(TM) 64-Bit Server VM (24.65-b04 mixed mode linux-amd64 compressed oops) Problematic frame: V [libjvm.so+0x657a7b] JNI_ArgumentPusherVaArg::JNI_ArgumentPusherVaArg(_jmethodID*, __va_list_tag*)+0x1b

Core dump written. Default location: /home/manish/rathi/libdmc/dmcore/include/core or core.25307

An error report file with more information is saved as: /home/manish/rathi/libdmc/dmcore/include/hs_err_pid25307.log

If you would like to submit a bug report, please visit:
http://bugreport.sun.com/bugreport/crash.jsp

Aborted (core dumped)

最佳答案

您找到constructor方法ID;但你声称这个类没有构造函数。这意味着 GetMethodID(env,local,"<init>","(V)V")将返回0。要为不带参数的方法指定签名,请使用 "()V" .

关于java - jni getMethodID 用于获取构造函数 ID 时返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26656653/

相关文章:

java - 如何通过java程序通过代理连接网站

java - 链接到 Javadoc 中的枚举成员

java - Java 文件名中应该或必须

java - JNA - 无法获得从 native 函数到回调函数的调用

java - JNI printf 到 log4j

Java JNI - 返回 jintArray 时出现 EXCEPTION_ACCESS_VIOLATION

java - 如何在不依赖的情况下从不同项目中的包中获取所有类?

java - 错误 `/usr/bin/java' : munmap_chunk(): invalid pointer: in JNI

android - 来自 WeakGlobalRef 的 JNI LocalRef

android - JNIEnv 全局引用与 C 中的 jobject 有何不同?