Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Calling back to C when started with invocation API

Thread view: 
naleiden - 20 Jun 2006 17:55 GMT
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
Gordon Beaton - 20 Jun 2006 18:07 GMT
> 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

Signature

[  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

naleiden - 20 Jun 2006 20:07 GMT
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 - 20 Jun 2006 20:08 GMT
Thanks for the further clarification Chris!
Chris Uppal - 20 Jun 2006 19:24 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.