call jni function dynamically without getting a JNIEnv handle as anargument.

J

junyoung

My application is using the jni interface to link java with c.

but, there is problem to use it. that is,

in side of the jni, I set a callback function to catch errors and
print it. this callback function tasks error informations.
and additionally, I can't expect the time this callback function wil
be called.

this is the simple structure of my code.

java:

ret = SetErrorHandle();

private native SetErrorHandle();

c:
static int generateException(void *aArg)
{
JNIEnv *sEnv = (JNIEnv *)aArg;
jclass sClass = (*aEnv)->FindClass("MyException");

sClass->ThrowNew(sEnv, "Error is here");
}

JNIEXPORT jint SetErrorHandle(JNIEnv *aEnv, jclass aClass)
{
/* this function will be called if there is an error */
/* first argument : function pointer
second argument : argument for function pointer
*/
setCallback(generateException, aEnv)
}

in this code, there are critical problems.
first one is sEnv variable pointer is not available in
generateException function.
whenever this function is called, it generated a segmentation fault
because the pointer is not AVAILABLE.

so my idea is to create new JNIEnv variable dynamically in
generateException function.

how do u think? if it is possible, how APIs I can reference?

thanks u.
 
E

EJP

private native SetErrorHandle();

This isn't legal Java as there is no return type, but from this evidence
it's not static.
sClass->ThrowNew(sEnv, "Error is here");

That's not the correct syntax, and it's also a terrible error message.
Surely the system you're interfacing to is providing some information of
its own?
JNIEXPORT jint SetErrorHandle(JNIEnv *aEnv, jclass aClass)

If this method is non-static this is the wrong signature for it. Have
you changed that without re-running javah? Also it's defined as 'jint',
which means that the native method returns 'int', but that's not what
you posted above.
setCallback(generateException, aEnv)

You can't do that. A JNIEnv* is only valid within the JNI function it is
supplied to. Specifically it is not valid across thread boundaries.
in this code, there are critical problems.
Yep.

first one is sEnv variable pointer is not available in
generateException function.
whenever this function is called, it generated a segmentation fault
because the pointer is not AVAILABLE.

.... whatever AVAILABLE means. The real reason is that the JNIEnv* value
you are arranging to supply to it is no longer valid - see above.
so my idea is to create new JNIEnv variable dynamically in
generateException function.

You can do that with AttachCurrentThread().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top