本人是一位小C程序员,最近工作需要,要调在c中用一些java程序(linux),使用JNI(网上查到的)调用比如:java某一个类中有一个函数:public void put(short c, int address){
 p[address]=(char) c;
}那么,我在c程序中应如何调用?我是这么调用的:
JNIEXPORT void JNICALL Java_JniTest_getLicence(JNIEnv *pEnv, 
                                      jobject test, 
                                      jobject obj)
{

        ........
jmethodID mid  = NULL;
jclass cls = (*pEnv)->GetObjectClass(pEnv, obj);
        mid =(*pEnv)->GetMethodID(pEnv, cls,"put","(S,I)V");
(*pEnv)->CallVoidMethod(pEnv, obj,mid,(jshort)MemoBuffer[j],j);

}
程序报错,但是如果是调用一个参数的接口是没问题的:
        jmethodID mid  = NULL;
jclass cls = (*pEnv)->GetObjectClass(pEnv,obj);
mid = (*pEnv)->GetMethodID(pEnv,cls,"setId","(J)V");
(*pEnv)->CallVoidMethod(pEnv,obj,mid,(jlong)m_IDNum);
return  m_IDNum;java接口是别的组开发的,不能修改,我知道这个问题对于各位java的大侠们说应该很菜,可实在是不懂java,惭愧!!
顺便问问,如果接口函数中参数是一个类的话,我该怎么调用,如果是好几个呢?
(分数不多了!!)

解决方案 »

  1.   

    应该是method descriptor写错了jclass cls = (*pEnv)->GetObjectClass(pEnv, obj);
            mid =(*pEnv)->GetMethodID(pEnv, cls,"put","(SI)V"); Method descriptors are formed by placing the field descriptors of all argument types in a pair of parentheses, and following that by the field descriptor of the return type. There are no spaces or other separator characters between the argument types. "V" is used to denote the void method return type. Constructors use "V" as their return type, and use "<init>" as their name.Here are some examples of JNI method descriptors and their corresponding method and constructor types.Method Descriptor
    Java Language Type
    "()Ljava/lang/String;"
    String f();
    "(ILjava/lang/Class;)J"
    long f(int i, Class c);
    "([B)V"
    String(byte[] bytes);
      

  2.   

    ok!解决了,zealVampire兄说的对!
    是函数的描述错了。原来可以通过javap 这个命令可以查看某个函数的描述。
    结贴了!!