[ JCCF ]
[ java APIs ]
[ JNI Basic Features ]
JNI Basic Features
We start with a simple Register.
The native keyword is used to indicate that the method code
does not correspond to byte code. Note that the language actually used
is not indicated. All we need to know, at that point, is that that
can be dynamically loaded.
The loading of the native code is performed by the instruction
System.loadLibrary("Register");. This code is in a
static block,
which means that it will be executed only when the Register class
will be loaded by the runtime.
In order to implement these native methods, we need to have their
prototype. The JNI defines a naming that enables the runtime to find
the native code of a method given its name. The file
Register.h, which contains these prototypes, can
be produced automaticaly with a utility such as javah. Note that both
the name of the class and the name of the method are used to build that name.
Now, we can implement these methods, this Register.cc
is wrote in C++. We use a JNIenv to dialog with the java runtime. Since
the internal structure of the Java objects must be keeped unknown, we refer to
the Java methods and object fields through identifiers. These identifiers are retrieved
from the JNIenv class (or structure, in C) thanks to the class descriptor, name and
signature of the field.
Note that in a real world implementation, we should perform error managment and that
we could cache the value of these idenitifiers in order to save the lookup overhead.
The reader can refer to the full Java Native Interface Specification for complete list of features.
Here is the Makefile used to build the example on a Solaris system using g++.
Links to source code :
Makefile
Register.h
UseRegister.java
Register.java
Register.cc
©
Alain.Miniussi@labri.u-bordeaux.fr
Last updated = june 12th 1997
url =
native/registerV1/index.html