JNI?

A

Alexander Adam

Hi,

I was searching a long time but didn't find a solution, some sample or
anything..

Our C++ Stuff (a graphic engine) can be compiled on windows, linux and
others and I heard about
using JNI but I am not quite
sure how that works. For example we cannot produce our header file with
javah from a*.java sourcefile as our
header file is already there and we cannot just change it, so how to
implement that in java?
The next issue is that the dll creates a visual context where you can draw
something. The issue is, how should that
work on Java / Eclipse ? I mean, somehow the dll needs to catch all the
mouse events, keyboard events etc. and redraw
itself at the right time etc.. How is that possible? Or is it even possible?

Alex
 
G

Gordon Beaton

Our C++ Stuff (a graphic engine) can be compiled on windows, linux
and others and I heard about using JNI but I am not quite sure how
that works. For example we cannot produce our header file with javah
from a*.java sourcefile as our header file is already there and we
cannot just change it, so how to implement that in java?

There can be more than one header file. The one generated by javah has
a different purpose than any header files you've written as part of
your graphic engine.

JNI allows you to define a number of methods in a Java class and
implement those methods in C or C++ (for example). The header file
generated by javah is there to describe the native methods that are
expected by the class.

Note that functions in your existing graphic engine cannot be invoked
directly from Java. You will need to write a JNI layer where you
implement methods that follow the rules of JNI (e.g. method names and
signatures found in the header generated by javah), and then calls the
corresponding functions in your graphics engine.

There is a JNI tutorial java.sun.com, and JNI documenation is included
in the JDK documenation package.
The next issue is that the dll creates a visual context where you
can draw something. The issue is, how should that work on Java /
Eclipse ? I mean, somehow the dll needs to catch all the mouse
events, keyboard events etc. and redraw itself at the right time
etc.. How is that possible? Or is it even possible?

It should be possible.

/gordon
 
C

Chris Uppal

Alexander said:
Our C++ Stuff (a graphic engine) can be compiled on windows, linux and
others and I heard about
using JNI but I am not quite
sure how that works. For example we cannot produce our header file with
javah from a*.java sourcefile as our
header file is already there and we cannot just change it, so how to
implement that in java?

You may be misunderstanding what JNI is, or how it works. Assuming that you
are wanting to call 'C' (or whatever -- but it has to be call-compatible with
'C') code from Java, which is the most common case, you must realise that /Java
is in control/. You are allowed to declare certain methods "native" and
provide an implementation of those methods in a DLL (or .so). Since the Java
runtime implementation is going to be invoking the functions in that DLL, you
/must/ provide those functions in a way that follows the conventions that Java
imposes. You cannot directly call arbitrary code.

So, typically, if you have some "legacy code" (as the Java docs call it --
which is kind of impertinent) you will create a "glue" DLL that is callable
from Java, and which in turn calls your existing 'C' code.

I recommend working through the JNI tutorial on Sun's website; that doesn't
take long and should make the mechanics much clearer.

The next issue is that the dll creates a visual context where you can draw
something. The issue is, how should that
work on Java / Eclipse ?

Java AWT provides an extension mechanism, called the 'AWT Native Interface'
(you'll find it alongside the JNI documentation in the docs that are part of
the JDK). I have never used it myself, but I gather that it is an extension to
JNI that gives a way for native code to provide the implementation of the
canvas's paint() method. I /think/ that you'll find that you have to do all
the event handling (mouse clicks, etc) in Java code, and (somehow) call your
existing event handling from that. My uninformed guess is that you'll have to
do quite a lot of refactoring of you existing code unless it already has a
separation between interpreting user-input and the underlying operations that
are invoked by it.

As far as Swing goes, I have no idea what facilities are available, the 'AWT
Native Interface' is not applicable, but there may be a comparable feature in
there somewhere.

-- 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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top