JNI - how to manage C++ objects from Java?

M

Martin Maercker

Hi,

I will soon need to reimplement parts of an existing object model in a
mixture of Java and C++ code. I leafed through "The Java Native
Interface - Programmer's Guide and Specification" but am still not
sure whether JNI can do what I need, especially as regards the
creation and management of C++ objects from Java code (the creation of
Java objects from C++ code, on the other hand, seems to be standard
procedure, at least as far as I could tell from the JNI book...).

What I need to happen on the C++ side is:
- Set up a singleton which starts a native timer thread.
- Listener objects register with the singleton.
- At each timer thread callback, a specific method is called in each
listener.
- At different times in the course of the app's lifecycle, individual
listeners need to be unregistered and destroyed and other listeners
created and registered.

These management tasks are driven by user interaction. Most of the
app, including the UI, is in Java, so I need to find a way to control
the C++ class lifecycles and relationships from Java.

Is this kind of setup possible with JNI? If so, could someone give me
some advice or direct me to reference material on how best to go about
it? If not, does anyone know an alternative?


Any help greatly appreciated,

Martin Maercker
 
M

Mariano Alonso Ortiz

Hi,
I encourage you to use CORBA as a glue. Code in C++ the server side
(skeleton) and use Java for the client side (stub). JNI is best suited for C
than for C++, you loose C++ OOP capabilities. In process or in the same
computer CORBA applications have a nice performance, transparent IPC and no
firewalling problems.

Good luck,

Mariano
 
C

Chris Uppal

Martin said:
I will soon need to reimplement parts of an existing object model in a
mixture of Java and C++ code. I leafed through "The Java Native
Interface - Programmer's Guide and Specification" but am still not
sure whether JNI can do what I need, especially as regards the
creation and management of C++ objects from Java code

As far as I can tell from your description, the problems you may face are (in
no particular order):

1) You may have to find a represention of a "pointer" to a C++ object that can
be held in Java code. The normal technique is to use an int or long for this
(which means that you have to cast between integer types and pointers in your
C/C++ JNI code -- but so what ?). An alternative is to keep
pointers/references in some array (on the C/C++ side) and simply pass around
indexes into that array on the Java side.

2) You may (depending on your application) have to manage object lifetime from
Java. I.e. if the Java code is the only part of the system that "knows" when a
C++ should be destructed then you will have to use finalisation (or
equivalently a reference queue) to manage that. You can fairly hope that this
will not be necessary.

3) You cannot call C++ virtual method from Java. You will end up putting a
real 'C' style wrapper around your C++ code. (This may be explicit, or it may
just be implicit in the way your JNI interface code works).

4) JNI is sensitive to threads. You cannot use JNI calls on any thread that
the JNI runtime does not know about. Pay especial attention to the threading
stuff in the JNI documentation (including the JNI book available on Sun's Java
website).

None of this is particularly difficult. But I advise you first to spend a
little while working through Sun's JNI tutorial (with real code written by
yourself -- not cut and pasted in).

-- chris
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top