Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > call jni function dynamically without getting a JNIEnv handle as anargument.

Reply
Thread Tools

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

 
 
junyoung
Guest
Posts: n/a
 
      04-27-2010
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.
 
Reply With Quote
 
 
 
 
EJP
Guest
Posts: n/a
 
      04-27-2010
On 27/04/2010 3:02 PM, junyoung wrote:
> 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().
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JNI thread vs. native thread vs. JNI call bgabrhelik Java 0 09-29-2009 03:33 PM
JNIEnv * DGG Java 2 05-23-2007 11:52 AM
Possible to handle web requests without an ASPX page? i.e. have DLL handle request. jdlwright@shaw.ca ASP .Net 2 05-31-2005 05:42 PM
Using JNIEnv In Native Method cppaddict Java 3 07-21-2004 12:14 AM
Re: debugging a JNI library: where do I get the JNIEnv lucky Java 2 04-07-2004 06:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57