简体   繁体   English

枚举的JNI调用方法抛出异常

[英]JNI call method of enum throws exception

I have the following enum inside my java code :我的java代码中有以下枚举:

package jni;

public enum Codec2Mode {
    CODEC2_MODE_3200(0),
    CODEC2_MODE_2400(1),
    CODEC2_MODE_1600(2),
    CODEC2_MODE_1400(3),
    CODEC2_MODE_1300(4),
    CODEC2_MODE_1200(5),
    CODEC2_MODE_700C(8),
    CODEC2_MODE_450(10),
    CODEC2_MODE_450PWB(11);

    private int m_code;

    public int getCode()
    {
        return m_code;
    }

    Codec2Mode( int code )
    {
        m_code = code;
    }
}

I need to pass it to JNI and use a value returned by getCode() .我需要将它传递给 JNI 并使用getCode()返回的值。 For this I declare为此我宣布

public native void initialize(Codec2Mode mode);

And here is the way I'm trying to access it at C++ side :这是我尝试在 C++ 端访问它的方式:

JNIEXPORT void JNICALL Java_jni_Codec2Wrapper_initialize
  (JNIEnv * env, jobject, jobject mode)
{
   jclass enumClass = env->FindClass("jni/Codec2Mode");

   jmethodID getCodeMethod = env->GetMethodID(enumClass, "getCode", "(V)I");

   jint value = env->CallIntMethod(mode, getCodeMethod);

   std::cout << "Arg = " << value << std::endl;
}

From java I call codec2.initialize(Codec2Mode.CODEC2_MODE_2400);从 java 我调用codec2.initialize(Codec2Mode.CODEC2_MODE_2400); . . But I get segmentation fault.但我得到了分段错误。 What's could be an issue here?这里可能有什么问题?

You have incorrect signature here:您的签名不正确:

jmethodID getCodeMethod = env->GetMethodID(enumClass, "getCode", "(V)I");

It should read: "()I"它应该是: "()I"

It's how it is defined in your class.这就是它在您的类中的定义方式。

public int getCode();
    descriptor: ()I

you can get signature of the method like that你可以得到这样的方法的签名

> javap -cp . -s -p jni.Codec2Mode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM