[ JCCF ] [ java APIs ] [ Java Native Interface ]

JNI

In the Java framework, we call native method any method which is not wrote in Java (or, more precisely, which is not compiled in Java byte code).

Natives methods are used since the begining of Java. As an example, on a SPARC architecture, all the graphical interface object were, directly or not, wrappers around Motif objects.

But the first version (JDK1.0) was not really satisfying :
  • The internal structure of the java objects were exported to the native code (as a C structure).
  • It was based on the assumption a conservative garbage collector (a garbage collector that does not move the objects).
  • It didn't support overloaded native methods.
  • The call of Java code from native code had to be done by name (i.e. with two strings, one for the name and one for the signature).
  • As we can see, the main problem is that the native code is not independent from the JVM (Java Virtual Machine), the structure of the objects and the kind of garbage collection is not fixed by the langage specification and can change from one JVM to another. So, the native code that works with a given JVM can be useless with another JVM.

    The JNI (Java Native Interface) is an atempt to solve these problems. It proposes a framework in which native code doesn't have (and is discouraged) to make any assumption about the JVM, excepted the ones given by the specification. Still, native code is able to interact with the JVM for the langage specific features such as exceptions and multi-threading (specific in the sense that the langage used to write the native code might not have direct support for them).

    The JNI also enables native code to use Java programs and Java classes. Even if the startup is not done in Java. This is achieved by creating a new JVM at runtime.

    An interessing point is that we can combine JNI and RMI (Remote Method Invocation) to remotly call native code (but that solution must be compared to a CORBA based solution).

    We give five examples to illustrate the use of the JNI:
  • getting started
  • overloading
  • exceptions
  • multi-threading
  • using Java Objects
  • See also:

    The JavaSoft JNI tutorial
    http://java.sun.com:80/docs/books/tutorial/native1.1/index.html

    The Java Native Interface Specification
    http://www.labri.u-bordeaux.fr/java/sundoc/jdk1.1/docs/guide/jni/spec/jniTOC.doc.html



    © Alain.Miniussi@labri.u-bordeaux.fr
    Last updated = june 12th 1997
    url = native/index.html