Calling back to C when started with invocation API

Discussion in 'Java' started by naleiden, Jun 20, 2006.

  1. naleiden

    naleiden Guest

    Is it possible to call native methods in a Java program which has
    been started with the invocation API, where the navive methods are
    implemented in the same C program that started the Java, i.e.:

    /////////////////
    // C Code //
    /////////////////

    // start JVM from C
    void do_java_work () {
    JNI_CreateJavaVM(&jvm, &env, &vm_args);
    jclass cls = env->FindClass("Main");
    jmethodID mid = env->GetStaticMethodID(cls, "test", "()Z");
    env->CallStaticVoidMethod(cls, mid, 0);
    ...

    }

    JNIEXPORT jboolean JNICALL Java_doSomething (JNIEnv *env, jobject obj)
    {
    ...

    }

    // End C code..

    /////////////////////
    // Java Code //
    /////////////////////

    public class Main {

    public static void test (String args[]) {
    doSomething();
    }

    public native boolean doSomething ();

    }

    // End Java Code

    I know this will not work as currently coded, but can anyone tell me
    how to do it, or how to do something equivalent?

    Thanks,

    -Nick
     
    naleiden, Jun 20, 2006
    #1
    1. Advertising

  2. On 20 Jun 2006 09:55:46 -0700, naleiden wrote:
    > Is it possible to call native methods in a Java program which has
    > been started with the invocation API, where the navive methods are
    > implemented in the same C program that started the Java, i.e.:


    Yes. The only difference from doing it the "regular" way is that you
    don't need to load the library containing the native methods in order
    to call them.

    You still need to make sure your methods follow the JNI naming and
    calling convention (which kind of implies that you need to use javah),
    or you can use RegisterNatives() to associate your native methods with
    the class that will use them.

    > I know this will not work as currently coded, but can anyone tell me
    > how to do it, or how to do something equivalent?


    Except that it isn't complete, I can't see anything blatantly wrong
    with the example you posted.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jun 20, 2006
    #2
    1. Advertising

  3. naleiden

    Chris Uppal Guest

    naleiden wrote:

    > Is it possible to call native methods in a Java program which has
    > been started with the invocation API, where the navive methods are
    > implemented in the same C program that started the Java,


    Yes.

    You will probably want to use the JNI function RegisterNatives() to tell the
    JVM what implementation it should use for whatever native methods you define in
    your outer C program. (That's instead of the typical JNI pattern where the
    java class loads a library containing that code in its static initialiser).

    If you do that then names of the functions you use are irrelevant to the JVM
    (since it's working directly with function pointers instead of looking up
    functions by name in a DLL/.so file); but the parameters, calling convention,
    and return type should be /exactly/ as they are generated by javah.

    So in your example, you would use RegisterNatives() to tell the JVM that the
    implementation of Main.doSomething() was provided by the function pointed to by
    &Java_doSomething.

    -- chris
     
    Chris Uppal, Jun 20, 2006
    #3
  4. naleiden

    naleiden Guest

    Thanks for your quick reply Gordon! I feel compelled to tell you that
    recognizing your name (from many of your posts that I have used
    previously) gives me confidence in venturing forward! :D
     
    naleiden, Jun 20, 2006
    #4
  5. naleiden

    naleiden Guest

    Thanks for the further clarification Chris!
     
    naleiden, Jun 20, 2006
    #5
    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. lucas

    JNI- Invocation API problem

    lucas, Nov 15, 2004, in forum: Java
    Replies:
    1
    Views:
    474
    Thomas Weidenfeller
    Nov 15, 2004
  2. Gearóid O'Ceallaigh

    Calling requiredif on "save" invocation

    Gearóid O'Ceallaigh, Aug 7, 2008, in forum: Java
    Replies:
    1
    Views:
    258
    Gearóid O'Ceallaigh
    Aug 11, 2008
  3. Thomas Thomassen
    Replies:
    2
    Views:
    154
    Thomas Thomassen
    Dec 29, 2010
  4. Jake Barnes
    Replies:
    2
    Views:
    312
  5. Kenneth Miller

    JNI Invocation API example

    Kenneth Miller, Sep 25, 2013, in forum: Java
    Replies:
    4
    Views:
    366
    Roedy Green
    Oct 2, 2013
Loading...

Share This Page