Home > Store > Programming > Java

larger cover

Add To My Wish List

Java™ Native Interface: Programmer's Guide and Specification

Register your product to gain access to bonus material or receive a coupon.

  • Description
  • Downloads
  • Reviews
  • Sample Content

Product Author Bios

Sheng Liang, a staff engineer in Java Software at Sun Microsystems, Inc., designed the JNI and led the Java virtual machine development for the first release of the Java 2 platform. He holds a Ph.D. in computer science from Yale University.



0201325772AB04062001

The Java Native Interface (JNI) enables the integration of code written in the Java programming language with code written in other languages such as C and C++. It allows programmers to take full advantage of the Java platform without having to abandon their investment in legacy code.

This book is the definitive resource and a comprehensive guide to working with the JNI. Entirely up-to-date, the book offers a tutorial, a detailed description of JNI features and programming techniques, JNI design justifications, and the official specification for all JNI types and functions.

You will find coverage of important topics such as:

  • Writing native methods
  • Passing data types between the Java language and native programming languages
  • Embedding a Java virtual machine implementation in native applications
  • Leveraging legacy native libraries
  • Improving the efficiency and reliability of your code

An entire chapter is devoted to avoiding common traps and pitfalls. The book uses numerous examples to illustrate programming techniques that have proven to be effective.



0201325772B04062001

Source Code

Click below for Source Code related to this title:
Source Code

Customer Reviews

32 of 33 people found the following review helpful
4.0 out of 5 stars Dont Buy this Book It is Freely Available on the Java Site, April 1, 2002
By 
Vamsi P. (Redmond, WA USA) - See all my reviews
This review is from: The Java Native Interface: Programmer's Guide and Specification (The Java Series) (Paperback)
This is book is very handy if you are programming for JNI. I went and bought this book but later found that is free on the Sun's Java site.... So buy it only if you need a paper copy. If you are ok with a PDF or html copy goto sun's website to get it.
Help other customers find the most helpful reviews 
Was this review helpful to you? Yes No


11 of 11 people found the following review helpful
5.0 out of 5 stars A must for JNI users, June 11, 1999
By 
Rajiv Mirani (California) - See all my reviews
(REAL NAME)   
This review is from: The Java Native Interface: Programmer's Guide and Specification (The Java Series) (Paperback)
This is an excellent book! Liang provides short, illustrative, and useful examples as well as helpful tips for the JNI user. The presentation is extremely clear, with no fluff. This book is more informative than any existing material on JNI I know of. It offers a gentle introduction for the novice as well as technical depth for the expert. I particularly like the level of insight that only the designer and architect of the JNI can provide. My favorite programming technique is "shared stubs," beats J/Direct.
Help other customers find the most helpful reviews 
Was this review helpful to you? Yes No


16 of 18 people found the following review helpful
4.0 out of 5 stars Good book but do not buy it. It is available FREE from SUN., April 25, 2003
This review is from: The Java Native Interface: Programmer's Guide and Specification (The Java Series) (Paperback)
The reason I write this comment is that I was suprised
that this book is available in electronic form, free
of charge from Sun website.
As far as I read the book, it is quite good but I did not
test anything. I probably will in my summer project and I will
inform you if what the Native Interface promised , did
finally work.
Anyway the Sun website for the book is :

[website]

Help other customers find the most helpful reviews 
Was this review helpful to you? Yes No


Share your thoughts with other customers:
 See all 16 customer reviews...

Table of Contents

I. INTRODUCTION AND TUTORIAL.

1. Introduction.

The Java Platform and Host Environment. Role of the JNI. Implications of Using the JNI. When to Use the JNI. Evolution of the JNI. Example Programs.

2. Getting Started.

Overview. Declare the Native Method. Compile the HelloWorld Class. Create the Native Method Header File. Write the Native Method Implementation. Compile the C Source and Create a Native Library. Run the Program.

II. PROGRAMMER'S GUIDE.

3. Basic Types, Strings, and Arrays.

A Simple Native Method. Accessing Strings. Accessing Arrays.

4. Fields and Methods.

Accessing Fields. Calling Methods. Invoking Constructors. Caching Field and Method Ids. Performance of JNI Field and Method Operations.

5. Local and Global References.

Local and Global References. Freeing References. Rules for Managing References.

6. Exceptions.

Overview. Proper Exception Handling.

7. The Invocation Interface.

Creating the Java Virtual Machine. Linking Native Applications with the Java Virtual Machine. Attaching Native Threads.

8. Additional JNI Features.

JNI and Threads. Writing Internationalized Code. Registering Native Methods. Load and Unload Handlers. Reflection Support. JNI Programming in C++.

9. Leveraging Existing Native Libraries.

One-to-One Mapping. Shared Stubs. One-to-One Mapping versus Shared Stubs. Implementation of Shared Stubs. Peer Classes.

10. Traps and Pitfalls.

Error Checking. Passing Invalid Arguments to JNI Functions. Confusing jclass with jobject. Truncating jboolean Arguments. Boundaries between Java Application and Native Code. Confusing IDs with References. Caching Field and Method Ids. Terminating Unicode Strings. Violating Access Control Rules. Disregarding Internationalization. Retaining Virtual Machine Resources. Excessive Local Reference Creation. Using Invalid Local References. Using the JNIEnv across Threads. Mismatched Thread Models.

III. SPECIFICATION.

11. Overview of the JNI Design.

Design Goals. Loading Native Libraries. Linking Native Methods. Calling Conventions. The JNIEnv Interface Pointer. Passing Data. Accessing Objects. Errors and Exceptions.

12. JNI Types.

Primitive and Reference Types. Field and Method Ids. String Formats. Constants.

13. JNI Functions.

Summary of the JNI Functions. Specification of JNI Functions.

Index. 0201325772T04062001

Preface

This book covers the Java(TM) Native Interface (JNI). It will be useful to you if you are interested in any of the following:

  • integrating a Java application with legacy code written in languages such as C or C++
  • incorporating a Java virtual machine implementation into an existing application written in languages such as C or C++
  • implementing a Java virtual machine
  • understanding the technical issues in language interoperability, in particular how to handle features such as garbage collection and multithreading

First and foremost, the book is written for developers. You will find easy steps to get started with the JNI, informative discussions on various JNI features, and helpful tips on how to use the JNI effectively. The JNI was initially released in early 1997. The book summarizes two years of collective experience gained by engineers at Sun Microsystems as well as the vast number of developers in the Java technology community.

Second, the book presents the design rationale of various JNI features. Not only is this of interest to the academic community, but a thorough understanding of the design is also a prerequisite to using the JNI effectively.

Third, a part of the book is the definitive JNI specification for the Java 2 platform. JNI programmers may use the specification as a reference manual. Java virtual machine implementors must follow the specification to achieve conformance.

Send comments on this specification or questions about JNI to our electronic mail address: jni@java.sun.com. For the latest on the Java 2 platform, or to get the latest Java 2 SDK release, visit our web site at http://java.sun.com. For updated information about The Java(TM)Series, including errata for this book, and previews of forthcoming books, visit java.sun.com/Series.

The JNI was designed following a series of discussions between Sun Microsystems and Java technology licensees. The JNI partly evolved from Netscape's Java Runtime Interface (JRI), which was designed by Warren Harris. Many people from Java technology licensee companies actively participated in the design discussions. They include Russ Arun (Microsoft), Patrick Beard (Apple), Simon Nash (IBM), Ken Root (Intel), Ian Ellison-Taylor (Microsoft), and Mike Toutonghi (Microsoft).

The JNI design also benefited greatly from Sun internal design reviews conducted by Dave Bowen, James Gosling, Peter Kessler, Tim Lindholm, Mark Reinhold, Derek White, and Frank Yellin. Dave Brown, Dave Connelly, James McIlree, Benjamin Renaud, and Tom Rodriguez made significant contributions to the JNI enhancements in Java 2 SDK 1.2. Carla Schroer's team of compatibility testers in Novosibirsk, Russia, wrote compatibility tests for the JNI. In the process they uncovered places where the original specification was unclear or incomplete.

The JNI technology would not have been developed and deployed without the management support of Dave Bowen, Larry Abrahams, Dick Neiss, Jon Kannegaard, and Alan Baratz. I received full support and encouragement to work on this book from my manager Dave Bowen.

Tim Lindholm, author of The Java(TM) Virtual Machine Specification, led the Java virtual machine development effort at the time when the JNI was being designed. Tim did pioneering work on the virtual machine and native interfaces, advocated the use of the JNI, and added rigor and clarity to this book. He also provided the initial sketch for this book's "kitchen and dining room" cover art design.

This book benefited from the help of many colleagues. Anand Palaniswamy wrote a portion of Chapter 10 on common traps and pitfalls. Janet Koenig carefully reviewed a preliminary draft and contributed many useful ideas. Beth Stearns wrote a draft of Chapter 2 based on the online JNI tutorial.

I received valuable comments on a draft of this book from Craig J. Bordelon, Michael Brundage, Mary Dageforde, Joshua Engel, and Elliott Hughes. Lisa Friendly, editor of The Java(TM) Series, was instrumental in getting this book written and published. Ken Arnold, author of The Java(TM) Programming Language, first suggested that a JNI book be written. I am indebted to Mike Hendrikson and Marina Lang at Addison-Wesley for their help and their patience throughout the process. Diane Freed oversaw the production process from copy editing to final printing.

In the past several years I have had the privilege of working with a group of talented and dedicated people in Java Software at Sun Microsystems, in particular members of the original, HotSpot, and Sun Labs virtual machine teams. This book is dedicated to them.

Sheng Liang

May 1999

0201325772P04062001

 
Buy

Book  $44.99  $35.99

Usually ships in 24 hours.

This book includes free shipping!

Purchase Reward: One Month Free Subscription
By completing any purchase on InformIT, you become eligible for an unlimited access one-month subscription to Safari Books Online.

Get access to thousands of books and training videos about technology, professional development and digital media from more than 40 leading publishers, including Addison-Wesley, Prentice Hall, Cisco Press, IBM Press, O'Reilly Media, Wrox, Apress, and many more. If you continue your subscription after your 30-day trial, you can receive 30% off a monthly subscription to the Safari Library for up to 12 months. That's a total savings of $199.