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

Discussion in 'Java' started by junyoung, Apr 27, 2010.

  1. junyoung

    junyoung Guest

    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.
    junyoung, Apr 27, 2010
    #1
    1. Advertising

  2. junyoung

    EJP Guest

    Re: call jni function dynamically without getting a JNIEnv handleas an argument.

    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().
    EJP, Apr 27, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lucky
    Replies:
    2
    Views:
    680
    Gordon Beaton
    Apr 7, 2004
  2. cppaddict

    Using JNIEnv In Native Method

    cppaddict, Jul 20, 2004, in forum: Java
    Replies:
    3
    Views:
    15,670
    cppaddict
    Jul 21, 2004
  3. DGG

    JNIEnv *

    DGG, May 22, 2007, in forum: Java
    Replies:
    2
    Views:
    807
    Thomas Fritsch
    May 23, 2007
  4. bgabrhelik
    Replies:
    0
    Views:
    787
    bgabrhelik
    Sep 29, 2009
  5. Adam Lipscombe
    Replies:
    1
    Views:
    81
    scripts.contact
    Jun 20, 2007
Loading...

Share This Page