Essential Jni: Java Native Interface

(NOTE: Each chapter beings with an Introduction and concludes with a Summary.) Preface. What This Book is About. Who Should Read This Book? Structure of This Book. Whose Java? Downloading Example Source. Building the Examples. Setting Up the Environment. How To Build. A Word About the Examples. Some Conventions. Help From Fellow Travelers. A Word About Borrowed Words. Acknowledgments. 1. Why JNI? Virtual Machine, Real Machine. JNI and Binary Compatibility. Memory Loss. The JNI Solution. Features of the JNI. 2. Hello JNI World. The Big Picture. The Steps. Identify Native Functionality. Describing the Interface to the Native Code. Writing the Java Code. Using javac to Generate Class Files. Using javah to Generate Include Files. Writing Native Code. Building a Library. Loading and Invoking the native Method. Building and Running Your Java Application. The Steps in Pictures. 3. Setting Java Variables, Invoking Java Methods. What Is All This Talk About C and C++? Some of the Basics. Setting Java Object Data Fields. Native Access to an Objects Own Data Fields. Making It Happen. Native Access to Another Object's Data Fields. Making It Happen. Getting a Java Object's Data Fields. Invoking a Java Method. Native Access to an Objects Own Methods. Native Access to Private Methods of Another Object. Making It Happen. A Disclaimer of Sorts. Returning a Value From Java Methods. Non-Virtual Method Invocation. Use of JNI Non-Virtual Functions. Yet More Ways to Call a Java Method. 4. Native Types, Signatures and Other Details. Java Types. Java Native Types. Primitive Native Types. Reference Native Types. Programming Considerations. The jvalue Type. Field and Method Identifier Types. Method Signatures. Signature and Constructors. Type Signatures and Data Fields. The JNIEnv Pointer. A Word About Passing Arguments. 5. Objects and Classes. JNI References. Code Natively, Reference Locally. Coding Globally. Releasing a Local Reference. A Few More Facts About Native References. JNI Object Functions. Determining an Object's Class. Creating a New Object Instance. Using NewObject. Using NewObjectA. Using NewObjectV. Example Object Creation. Allocating Memory for an Object. Comparing Two References. A Native instanceof. Testing Class Inheritance Relationship. JNI Class Functions. From Class Name to Class. From Class to Superclass. From Bytes to Class. A Simple Class Loader. A Loader Using DefineClass. 6. Arrays and Strings. JNI Array Functions. Object Arrays. Array of Arrays. Some Output. Primitive Type Arrays. Creating a Primitive Array. Getting Primitive Array Elements. Accessing the Entire Array Contents of a Primitive Array. An Example: Accessing an Entire Array. Accessing a Region of a Primitive Array. JNI String Functions. UTF-8 String Handling. Creating a New String Object. Unicode String Functions. 7. Exceptions. Throwing Exceptions. Throwing an Exception. Building and Throwing an Exception. When Does an Exception Get Raised? Catching Exceptions. A Native Try-Catch. Passing Exceptions to Java. A JNI Stack Trace. General Exception Handling Guidelines. 8. Monitors. JNI Synchronization Support. The Producer-Consumer Problem. A Java Implementation. A JNI Producer-Consumer Implementation. The C++ Product Info Class. The init Method. C++/Java Object Correspondence. Native Implementation of Produce Method. Native Implementation of Consume Method. A Native Wait and Notify. Native Producer-Consumer Output. A Reminder. A Global Reference as Lock. 9. Java and C++. C++ Legacy Code. The General Strategy. Mirroring Example. The C++ Side. The Java Side. Type Mappings. Object Construction. Finalization. Cloning. Serialization. The Native Methods. The Registry Interface. Object Construction and the Registry. Instance Data Access and the Registry. Object Finalization and the Registry. Object Cloning and the Registry. Serialization and the Registry. The Registry Implementation. MFC Implementation of ObjMap. Tools.h++ Implementation of ObjMap. Static Variables and Methods. C++ Class Methods. Public Data Field Access. Java Mirroring at Work 176 An Alternative to the Registry. Some Other Issues. Multiple Inheritance. C++ Templates. 10. Conversion of C Structures. The Java Source. The C Adapter Code. POSIX User Database Access. Using structConverter. Running the C Preprocessor. The structConverter Configuration File. Running structConverter. The Java Source File. StructConverter Data Type Conversion. InitFIDs and the static Block. The Adapter Source File. Field ID Initialization. Adapter Code. Tying It All Together. Another Database Example. Updating a Booking Record. Generating Just Java. Some Design Considerations. A Bit More About structConverter. 11. Native Serial I/O. The portio Package. Using portio. Portio Template Class Files. Augmenting portio. Portio and POSIX tty. The PosixPortDriver Class. The PosixPortReader Class. The PosixPortPrivate Class. The POSIX Native Code. Opening and Closing a Port Using POSIX System Calls. Reading and Writing a Port Using POSIX System Calls. Using POSIX System Calls flush, sendBreak and purge. Setting POSIX Device Parameters. Portio and Win32 COM. 12. Running the JVM From a C/C++ Application. JVM Initialization. Stack and Heap Values. Garbage Collection Settings. Load Verification Mode. The classpath Field. The properties Field (JDK 1.1.2 and Greater). Application Hooks for the JVM. Invocation API Overview. invokeMain: Creating a JVM. A Java-enabled Application in Action. Building a Java-Enabled Application. The Invocation API and Reflection. JNI and Reflection. Running miniInvoker. A Small Disclaimer. Registering Native Methods. The JNINativeMethod Structure. Using RegisterNatives. Building for Native Method Registration. Avoiding Static Libraries. 13. An NT Service Application. The Service Control Manager. Registering a Service With the SCM. The Service Process. The Executing Service. Reporting Service Status. The service_controller Routine. The Java Agent Server. Starting the Java Agent Server. Stopping the Java Agent Server. Support for Java Agent Creation. The JVM as Service: Some Considerations. The JVM's Current Working Directory. Displaying a Frame. Some Common Errors. A Java Agent Service. Overview of a Java Logging Agent. Source for a Java Logging Agent. Making It Happen. 14. Debugging Native Methods. What Is It You Are Debugging? JNI Debugging Tips. Microsoft Visual Studio. The JVM: Starting a Debug Session. Debugging on Solaris. Using dbx. Debugging With Sun Workshop. When All Else Fails. 15. JDK 1.2 Enhancements to JNI. Summary of Enhancements. Summary of Additional Functionality. JDK 1.2 JNI Enhancements. JVM Initialization. Improvements to Invocation API Functions. FindClass Enhancements. New JDK 1.2 JNI Functions. Weak References. "Critical Region" String and Array Manipulation. Local Reference Management. Core Reflection Support. String Character Extraction. New Invocation API Functionality. Loading and Unloading Notification. Native Library Management Via Class Loaders. Appendix A. JNI Reference. Notation. The Invocation API. Appendix B. 1.0 Native Methods vs. JNI. javah Usage. Using javah to Generate 1.0-Style Native Code. Using javah to Generate JNI Function Prototypes. Include File Requirements. The Pieces, Side-by-Side. Building Native Libraries. A printString Example. The Execution Environment. Function Specification and Naming. Data Types. Argument Handling. Signatures. Accessing Java Data Fields. Accessing Instance Data. Accessing Class Data. Invoking Methods. Invoking a Java Instance Method. Invoking a Java Class Method. Creating an Object. Exceptions. String Manipulation. Arrays. Garbage Collection Issues. Appendix C. structConverter Reference. Command Syntax. Options. Appendix D. javah Reference. Command Syntax. Options. Description. Environment Variables. Appendix E. Native Methods, Applets, Security. General JDK Security. Netscape Communicator Security. Netscape Capabilities API and Object Signing. Avoiding Signatures. Using Native Code With Applets. How Did the Native Library Get There?