ACCU Home page ACCU Conference Page
Search Contact us ACCU at Flickr ACCU at GitHib ACCU at Facebook ACCU at Linked-in ACCU at Twitter Skip Navigation

Search in Book Reviews

The ACCU passes on review copies of computer books to its members for them to review. The result is a large, high quality collection of book reviews by programmers, for programmers. Currently there are 1949 reviews in the database and more every month.
Search is a simple string search in either book title or book author. The full text search is a search of the text of the review.
    View all alphabetically
Title:
The Java Native Interface
Author:
Sheng Liang
ISBN:
0 201 32577 2
Publisher:
Addison-Wesley
Pages:
303pp
Price:
£29-99
Reviewer:
Peter Pilgrim
Subject:
java
Appeared in:
12-5
If you have ever wondered why Java cannot change directory like you can in C++, or have been frustrated by Java's lack of access to the underlying file permissions, then you may have thought if there was any chance of extending Java to support these features. Perhaps you are prepared to give up the class portability just to have some native mission control feature. If you are looking for a book to teach you how to achieve that interface between Java and C/C++ relatively quickly then this book, The Java Native Interface, is definitely for you. JNI allows the integration of code written in C and./or C++ with code written in Java. This book is part tutorial, part reference and resource guide.

To start with the book assumes that you already know quite a bit about Java, so there is no introduction to the Java language to be found here. Of course you also have to know at least one of C or C++. The first chapter describes what the JNI API is and how it evolved. The author correctly warns about the implication of programming using JNI. Your final code will no longer be pure Java unless you port the native portion of your JNI feature to other machine. There is good solid advice as to when to use the Java Native Interface. The file permission access and modifying scenario at the top of this review is one valid case of using JNI where the alternatives are weaker and error prone.

Chapter two is about getting started with JNI proper. It essentially covers the JNI version of the Hello World in Java and C. It demonstrates the 'javah' compiler and how to use it so that a valid C/C++ header is created for your custom JNI applications. There are six steps to create this JNI hello world heaven. All very well described. A problem with this chapter is that the compilation of the 'C' native part covers only the Microsoft Visual C++ suite and the Solaris workshop compiler. No examples are supplied for either the Linux GNU compiler or Borland compilers, you are left to forage on the Internet I am afraid. The author does mention that the native portion must be a shared library, a shared object file (*.so) on UNIX platforms or dynamic link library (*.DLL) under Windows. He neglects to announce that library must be position independent and any library or executable linked against it must be relocatable, but this is not Java's fault, really.

The third chapter describes the basic methods, types, strings and array type that are available to C/C++ sections of JNI. Java language types are mapped to JNI Java types and typedefed to be C portable. String and array types are treated specially. The author informs us that the

String
type is Unicoded on the Java platform and that the arrays have to be handled using JNI API. There is no direct access to elements of Java array type. The next chapter shows how to access individual fields and methods in a Java object from the native side. JNI permits this by asking for special identification for each attribute; namely each field data member and method member will have a special ID. Every time you need access to a Java entity, be it a data member, method member, string or array object elements you need to have an ID, which can be thought of reference handle in the higher sense.

The idea of references to something called X is maintained in chapter five, which is a lesson on local and global references. By holding onto a global reference on the JNI C side of your new programming feature you can prevent the whole Java object or class from ever being garbage collected in the lifetime of the program JVM. However there is a fly in the ointment called a weak reference.

Java has particularly useful exceptions and chapter six demonstrates how to throw one from the native C side and also, perhaps just as important, how to catch one. The later chapters of the book describe how to embed the virtual machine in a native running application of your choice. The virtual machine can be created and called from C program. You can write a custom KDE application that is able to run any Java application natively. Well maybe not. The advanced chapters also describe the architecture of peer classes on the Java platform, one-to-one mapping and shared Java native stubs. Chapter 109 describes common traps and pitfalls, which is rather useful. The last part deals with the full JNI specification. This is a very good book.