Essential JNI: Java Native Interface

Essential JNI: Java Native Interface

by Rob Gordon
     
 

Leverage platform-specific features without compromising binary compatibility!

With the Java Native Interface API, you get the best of both worlds: access to the native platform features you need to get the job done plus full binary compatibility across multiple Java Virtual Machines. And there is no better way to master the JNI than this fast-paced, example-rich

Overview

Leverage platform-specific features without compromising binary compatibility!

With the Java Native Interface API, you get the best of both worlds: access to the native platform features you need to get the job done plus full binary compatibility across multiple Java Virtual Machines. And there is no better way to master the JNI than this fast-paced, example-rich guide, which focuses on proven techniques.

Walk step-by-step through writing Java and native code components, building libraries, loading and invoking native methods, and more. Discover powerful techniques for creating Java Virtual Machines from within any native application. Learn proven approaches to integrating Java and C++ code, converting C structures, and providing serial I/O from Java on both POSIX and Win32 platforms. Get comprehensive debugging help. There's even a complete, easy-to-use API reference!

With JNI, you can leverage Java's extraordinary capabilities without sacrificing proven legacy code.

Essential JNI: Java Native Interface gives you the insight, proven techniques, and real-world sample code you'll need to get results with JNI, plus:

  • A complete, task-oriented reference to the entire Java Native Interface API
  • Tools and strategies for integrating legacy code with new Java applications
  • Extensive code examples and detailed debugging tips
  • Clear explanations of JNI native types, signatures, references, and object and class functions
  • Specific techniques for managing C++ code and converting C structures

Editorial Reviews


Fatbrain Review

Aimed at software engineers and Java programmers, this book teaches Java and C/C++ integration. With an introduction to the Java Native Interface (JNI) API, the authors cover the entire API for the JNI, including the enhancements introduced in release 1.2 of JDK. JNI helps users access native platform features and full binary compatibility across multiple Java Virtual Machines (JVM).

The first section gives detailed coverage of JNI API, with a JNI version of "Hello World," Java variables and Java types. It also includes by explaining the remaining JNI functions, including objects, arrays, exceptions and monitors. The second section explains how to use JNI to integrate Java code with non-Java code. Chapters 9 through 11 discuss Java and C++, C structure conversions and native serial I/O. Chapters 12 and 13 tackle the Invocation API, providing details on how to start a Java Virtual Machine mechanisms from a C/C++ application. They also discuss starting the JVM as an NT service.

Chapter 14 concentrates on debugging Java applications with native code. The final chapter is devoted to changes and enhancements to JNI in the SDK 1.2. The appendices contain JNI references, 1.0 native methods versus JNI and StructConverter references. This is followed by a JAVAH reference and a brief discussion of native methods, applets and security issues.

Product Details

ISBN-13:
9780136798958
Publisher:
Prentice Hall Professional Technical Reference
Publication date:
03/04/1998
Series:
Essential Series for Web Professionals
Pages:
528
Product dimensions:
6.10(w) x 8.97(h) x 0.93(d)

Read an Excerpt

PREFACE:

Before we plow an unfamiliar patch It is well to be informed about the winds, About the variations in the sky, The native traits and habits of the place, What each locale permits, and what denies Virgil The Georgics

Preface

What This Book is About
The subject of this book is the Java Native Interface (JNI) Application Programming Interface (API). The JNI was introduced in release 1.1 of the Java Development Kit (JDK) as distributed by JavaSoft. This book covers the entire API for the JNI including the enhancements introduced in release 1.2 of the JDK. Where there are minor differences between various 1.1 point releases, these are discussed.

Who Should Read This Book?
This book is written for the software engineer who needs to make Java and C or C++ talk to one another. Experience with C/C++ and Java is assumed. This book also assumes some familiarity with both UNIX and Win32 platforms.

If you are a Java programmer who needs to step outside the Java Virtual Machine to take advantage of some platform-specific functionality, this book will show you how. If you are a C programmer responsible for putting a Java font-end on a legacy application, this book will show you how. If you are a C++ programmer wanting to take advantage of an existing C++ class library, this book will show you how.

Further, this book covers these topics for both UNIX and Win32 platforms.
Okay, now turn around and walk to the sales counter.

Structure of This Book

This book can be thought of as having three distinct parts. The first part, roughly the first eight chapters, covers the JNI API in great detail. Thesecond part, the remaining chapters, covers some general issues involved with native method programming. The third part, a series of appendices, contains reference material, both for the JNI and for tools introduced in this book. There is also an appendix that compares the JNI with the old-style native method programming model introduced in JDK 1.0. The last appendix offers a brief discussion of native methods, applets and security issues.

The early chapters contain plenty of simple examples intended to highlight the essential features of the API. No attempt is made to place JNI function calls into large, complex examples that obscure their salient features.

The first part of the book takes a walk-before-you-run approach. After an overview of the JNI in Chapter 1.
Chapter 2 presents a JNI version of the classic "Hello World" example.
Chapter 3 then follows with examples of some of the more common JNI operations before plunging into the syntactical details of the JNI in Chapter 4.
With all that work behind you, Chapter 5 through Chapter 8 provides detailed coverage of the remaining JNI functions.

The second half of the book deals with a series of general topics on using the JNI to integrate Java code with non-Java code. Chapter 9 presents an approach for mirroring existing C++ classes in Java. Chapter 10 introduces a tool for the automatic conversion of C structures into Java classes and an accompanying set of adapter functions for copying data between an instance of a C structure and a Java object. Chapter 11 starts with a collection of Java classes that provide a high-level interface to serial and parallel ports. Throughout this chapter the native code for targetting these classes for both POSIX and Win32 platforms is presented and discussed. The Java package used in this chapter is the portio package which is freely-available from Central Data (www.cd.com) as well as being included with the examples at the Prentice-Hall ftp site.

Chapter 12 and Chapter 13 deal with the Invocation API. Chapter 12 is a broad discussion of the mechanics involved with starting a Java Virtual Machine from a C/C++ application. Chapter 13 provides a very specific example of this facility, namely, starting the Java Virtual Machine (JVM) as an NT service. This chapter is for NT developers who wish to use Sun's JVM as an engine for their Java applications. Chapter 14 presents some approaches to debugging a Java application that includes native code. Finally, Chapter 15 is dedicated to changes and enhancements to the JNI in the JDK 1.2.

Whose Java?
This book covers the Java Native Interface API. To use the Java Native Interface API, you will need to run your Java code on a Java Virtual Machine that supports the JNI. The list of JVM vendors supporting the JNI is growing, but a sure bet is the JVM distributed by Sun. The Sun JVM, the Java Core classes and the JNI are available as part of the Java Development Kit (JDK). The JNI is supported starting in release 1.1 of the JDK.

To download the JDK, surf to the JavaSoft download site: ...

Meet the Author


Rob Gordon is a software consultant who has worked for Motorola, convex Computers, and, most recently, Sun Microsystems. He has extensive experience both as a Java software developer and as a developer of educational materials on operating systems, device drivers, and programming languages.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >