Reputation: 1691
I am trying to use function overload when declaring JNI native functions.
The Java method is :
public native static void methodaaa(String type, int errorCode);
public native static void methodaaa(String type, byte[] byts);
Without overload, the code is shown as below:
JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}
And this works just fine.
Then I tried to add overload :
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){}
And this give me the error :
conflicting types for Java_com_xxx_yyy_JavaCallCpp_methodaaa
Then I did some research and it seems like I need to add a "__" to the end of the functions that I want to overload and also append the arguments Name mangling.
So I tried:
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){}
But it still not work, the error is :
No implementation found for native Lcom/xxx/yyy/JavaCallCpp;.methodaaa:(Ljava/lang/String;I)V
Is anybody know that how to write the JNICALL function name with a jstring as parameter or what I am doing wrong here?
Any advice will be appreciated, thanks :)
Update:
I found the link here :
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html
and then tried to modify my code :
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){}
But, still I am getting the same error :
No implementation found for native Lcom/xxx/yyy/JavaCallCpp;.methodaaa:(Ljava/lang/String;I)V
Upvotes: 5
Views: 4055
Reputation: 69
https://edux.pjwstk.edu.pl/mat/268/lec/lect10/lecture10.html
maybe this will help you
/* 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
Field descriptors of the primitive types are presented in the table.
Java type Field descriptor
boolean Z
byte B
char C
short S
int I
long J
float F
double D
Upvotes: 4
Reputation: 1
Additionally, JNI function names are C, not C++. They can not be overloaded.
Upvotes: 2
Reputation: 310980
Don't attempt to figure out JNI method signatures yourself. Use the output of javah
. It is never wrong.
Upvotes: 6