Calling back to C when started with invocation API

N

naleiden

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
 
G

Gordon Beaton

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
 
C

Chris Uppal

naleiden said:
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
 
N

naleiden

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
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top