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 / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

JNI - Reference C++ class in java

Thread view: 
codesrik@yahoo.com - 02 Jan 2007 14:49 GMT
Hey guys,
I am trying to implement a simple functionality of JNI and I dont seem
to get a solution to it.

I have a C++ class with its own instance variables. In Java, I call a
method to create a new object and initialize all the variables. Now the
issue I am facing is calling the next method on the same object
created. This has become more of an issue because of multithreading. So
the workaround was to store the C++ object somehow in the Java
environment. So any C++ method would retrieve act upon that object
after retrieving from JEnv.

is there a way to do this?
Gordon Beaton - 02 Jan 2007 15:17 GMT
> I have a C++ class with its own instance variables. In Java, I call
> a method to create a new object and initialize all the variables.
> Now the issue I am facing is calling the next method on the same
> object created.

There are a few ways to do this.

For example, store the C++ object in any suitable native structure
such as an array or hashtable, and store the index or key (as
appropriate) in the corresponding Java object. Include the index when
you invoke any of your native methods, so the correct object can be
looked up.

Or simply return the address of the C++ object as a long that you
store in the corresponding Java object. In your native methods, cast
back to the correct type before calling the C++ object method.

Finally, have a look at "peer classes" here:
http://java.sun.com/docs/books/jni/html/jniTOC.html

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

codesrik@yahoo.com - 02 Jan 2007 15:37 GMT
> Or simply return the address of the C++ object as a long that you
> store in the corresponding Java object. In your native methods, cast
> back to the correct type before calling the C++ object method.

Thnx Gordon. I will try implementing the "long" solution and see how it
goes.


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.