Java Reference
In-Depth Information
may be called just like any other Java method using the techniques described in
Section 22.9.
22.13 Conclusion
The Java Native Interface is a vital component of Java. The JVM itself calls native
methods to implement much of the functionality of Java. This has been a long and
dense chapter because JNI is a large subject area. One can do almost anything
in JNI that one can do in Java. Partly because of the generality of the design of
JNI and partly because many native languages are substantially less capable than
Java, the API for accessing JNI functionality is messy. In addition to writing the
native code itself, one must also know how to compile and link the native code
into a shared library object and how to gain access to that object from Java code.
We have described almost all of JNI, though a few advanced features that
you can read about in the JNI Specification have been omitted. The only subject
we have avoided completely is the Invocation API, not because it is any more
difficult to use than the rest of JNI but because it is less likely to be used in typical
scientific programs than the techniques described here for Java code to call native
methods.
The decision to use native methods should be made with great care. As can be
deduced from the length of this chapter, using JNI properly can require signifi-
cant effort. In addition, as emphasized in the introduction, using JNI necessarily
renders a Java application no longer platform portable. Recent versions of Java
often remove performance concerns as a valid justification for using JNI, while
accessing large legacy computer codes remains a valid reason. We urge you to
be sure that the use of native methods is really necessary before embarking on a
journey into JNI that can often be verbose, messy (and ugly).
22.14 Web Course materials
The code files for the various programs discussed above are available on the Web
Course, as well as some additional examples. We also give an example of linking
Java to a Fortran program via JNI and an intermediate C code.
References
[1] JNI documentation, http://java.sun.com/j2se/1.5.0/docs/guide/jni/ .
[2] Trail: Java Native Interface - The Java Tutorial , Sun Microsystems,
http://java.sun.com/docs/books/tutorial/native1.1/ .
[3] Sheng Liang, The Java TM Native Interface Programmer's Guide and Specification ,
Addison-Wesley. 1999. Available online at http://java.sun.com/docs/
books/jni/ .
[4] CWYGIN, www.cwygin.com .
Search WWH ::




Custom Search