![](/img/trans.png)
[英]JNI_OnLoad error: Unable to find decl for native function with signature ()Ljava/lang/String
[英]jni native function overload signature
我在聲明JNI本機函數時嘗試使用函數重載。
Java方法是:
public native static void methodaaa(String type, int errorCode);
public native static void methodaaa(String type, byte[] byts);
沒有重載,代碼如下所示:
JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}
這很好用。
然后我嘗試添加重載:
JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}
JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa(JNIEnv* env, jobject thiz, jstring type, jbyteArray buffer){}
這給了我錯誤:
conflicting types for Java_com_xxx_yyy_JavaCallCpp_methodaaa
然后我做了一些研究,似乎我需要在我想要重載的函數的末尾添加一個“__”,並附加參數Name mangling。
所以我嘗試過:
JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_I(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}
JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_B(JNIEnv* env, jobject thiz, jstring type, jbyteArray buffer){}
但它仍然無效,錯誤是:
No implementation found for native Lcom/xxx/yyy/JavaCallCpp;.methodaaa:(Ljava/lang/String;I)V
有人知道如何用jstring作為參數編寫JNICALL函數名,或者我在這里做錯了什么?
任何建議將不勝感激,謝謝:)
更新:
我在這里找到了鏈接:
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html
然后嘗試修改我的代碼:
JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_2I(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}
JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_2B(JNIEnv* env, jobject thiz, jstring type, jbyteArray buffer){}
但是,我仍然得到同樣的錯誤:
No implementation found for native Lcom/xxx/yyy/JavaCallCpp;.methodaaa:(Ljava/lang/String;I)V
不要試圖自己弄清楚JNI方法簽名。 使用javah
的輸出。 這絕不是錯的。
https://edux.pjwstk.edu.pl/mat/268/lec/lect10/lecture10.html
也許這會對你有所幫助
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class getter_number_GetNumber */
#ifndef _Included_getter_number_GetNumber
#define _Included_getter_number_GetNumber
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: getter_number_GetNumber
* Method: getNumber
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_getter_number_GetNumber_getNumber__
(JNIEnv *, jobject);
/*
* Class: getter_number_GetNumber
* Method: getNumber
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_getter_number_GetNumber_getNumber__J
(JNIEnv *, jobject, jlong);
/*
* Class: getter_number_GetNumber
* Method: getNumber
* Signature: (FF)F
*/
JNIEXPORT jfloat JNICALL Java_getter_number_GetNumber_getNumber__FF
(JNIEnv *, jobject, jfloat, jfloat);
#ifdef __cplusplus
}
#endif
#endif
表格中給出了基元類型的字段描述符。
Java type Field descriptor
boolean Z
byte B
char C
short S
int I
long J
float F
double D
另外,JNI函數名是C,而不是C ++。 他們不能超載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.