Java Reference
In-Depth Information
Chapter 22
The Java Native Interface (JNI)
22.1 Introduction
Chapter 20 developed a complete client/server application in which both the
client code and the server-side code were implemented completely in Java. Pure
Java on the client side is desirable for many reasons - platform portability, rich
user interface, object-oriented programming environment, ability to run as an
application, an applet or a Java Web Start application, etc. For many of the
same reasons, a pure Java server offers obvious advantages too. However, for
some calculation-intensive processing tasks, particularly if legacy code in another
language already exists, it can be advantageous for a Java program to gain access
to code written in another language such as C or Fortran.
Java permits calls to code written in languages other than Java. Such exter-
nal languages are referred to as “native” languages, and the API for access-
ing them is called the Java Native Interface, or JNI [1]. The decision to use
JNI should be made with great care. JNI is designed for use when it is nec-
essary to take advantage of platform-specific functionality that is not available
within the Java Virtual Machine. There are two key concepts in that previous
sentence - necessary and platform-specific functionality .Toutilize platform-
specific functionality via JNI obviously removes Java's platform portability. In
fact, any use of JNI at all renders a Java application no longer platform portable
since the Java application requires a native shared object library for each plat-
form to which it is targeted. In some cases, such as when the native code is
very generic or easily portable, a simple recompile of the native code on a new
platform can produce a working shared object library, and thus a working JNI
application for that platform. In many cases, though, the native code could be
difficult to port, particularly when the purpose of utilizing native code is to gain
access to platform-specific features of a particular platform. Porting among var-
ious versions of Unix or Linux is normally easy, but porting a library that uses
OS-specific functionality to or from a Microsoft Windows platform is often quite
difficult.
Because of the loss of platform portability, one should carefully consider
whether the desired platform-specific functionality is really necessary to the
585
Search WWH ::




Custom Search