0

I am implementing a C++ to Java interface using JNI. I have a C++ function as follows:

static int testhandler(void *arg, uint32_t stream, uint32_t func, const char* name, uint32_t funcgroup, uint32_t source);

When I have to call this in Java using eclipse, I do it as follows:

public void handle(int stream, int func,char name, int group, int token);

But I am not able to read the const char part in Java using Eclipse. Does anybody know what the problem might be? Should I call the method in Java in some other manner?

11
  • In Java, char is a UTF-16 encoded character. In C++, char is a byte, and char * is most likely a nul-terminated string. Commented Jun 4, 2013 at 9:39
  • Loot at electrofriends.com/qna/jni-faq/…. It should help. Commented Jun 4, 2013 at 9:41
  • 1
    At least testhandler doesn't look like a JNI function. Did you miss something we should know? Commented Jun 4, 2013 at 9:48
  • @Angew I use a Java function signature from C++ to Java as follows: "(IICII)V" Should I change the C to B(Byte) or String? I am not sure how to set signature for string though. Commented Jun 4, 2013 at 9:49
  • @BlueWanderer the JNI part is like this:JNIEXPORT void JNICALL Java_otf_OtfJni_otfjni_1set_1handler (JNIEnv *env, jobject obj, jint type, jobject handler) { struct callback *cb; jclass handler_class = (*env)->GetObjectClass(env, handler); cb = (struct callback *)malloc(sizeof(struct callback)); cb->env = env; cb->handler = (*env)->NewGlobalRef(env, handler); cb->id = (*env)->GetMethodID(env, handler_class, "handle", "(IICII)V"); printf("adding a handler\n"); OTF_HandlerArray_setHandler(handlers, testhandler, type); OTF_HandlerArray_setFirstHandlerArg(handlers, (void *)cb, type); Commented Jun 4, 2013 at 9:53

1 Answer 1

1

You can't just pass const char* string through JNI.

Create a jstring from it using function NewStringUTF (it is member-function of class JNIEnv). Then pass it to Java's String.

Sign up to request clarification or add additional context in comments.

6 Comments

I understand that I have to use NewStringUTF, but in my case, the const char* is part of a function call as seen in testhandler. So shud I use the NewStringUTF inside the function call or inside the JNIExport function?
@user2358330 Could you please give some code parts here for me to understand what do you mean?
The JNIExport function makes a call to testhandler which has the const char*. The JNI code is something like this: JNIEXPORT void JNICALL Java_otf_OtfJni_otfjni_1set_1handler (JNIEnv *env, jobject obj, jint type, jobject handler){ OTF_HandlerArray_setHandler(handlers, testhandler, type); } And the testhandler function is: { struct callback *cb = (struct callback *)arg; JNIEnv *env = cb->env; (*env)->CallVoidMethod(env, cb->handler, cb->id, stream, func, name, funcgroup, source); return (*env)->NewStringUTF(env, name); }
@user2358330 I can't get the logic. Why int testhandler is returning jstring? When I was answering the main question, I meant that parameter name of type char in java is not equivalent to const char* from C++. Here you need NewStringUTF to convert this parameter to jstring and give to java function, which gets String.
@user2358330 I thought of something like this: env->CallVoidMethod(......., env->NewStringUTF(name), .....).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.