The Raw Native Interface (RNI) is a set of functions and structures defined in native.h and nativcom.h. RNI provides an efficient way of both traversing between Java and native code, and manipulating objects while in native code.
Using RNI requires knowledge of garbage collection, pointer locking, and other issues. Dynamic-link libraries (DLLs) you call through RNI need to be specially written to work with RNI. You can't use RNI to call arbitrary DLLs, although you can write wrapper DLLs.
To work with RNI, native code must follow naming conventions, adapt to Java data representations, and deal with the garbage collection process of the Java environment.
For example, if the native code is going to be in a long loop, the garbage collector should be called periodically. Similarly, if the code is going to block on user input or call a thread that will block, it should call the garbage collector around the code that can block. Furthermore, objects need to be protected by garbage collection frames when calling back into Java.
All this increases programming complexity. In exchange for this complexity, you get greater speed and full access to all objects' fields. A good use of RNI is systems-level programming tasks, such as extending the capabilities of the Microsoft virtual machine (Microsoft VM).
The following topics describe the mechanism of calling between Java and native code (typically C or C++) using the Microsoft VM. This includes the native code DLL interface as well as implications in the mechanism for garbage collection.
Raw Native Interface Enhancements
RNI Message Box Example
Accessing Fields
Garbage Collection Issues
Calling Java Methods
Object Creation
Throwing Java Exceptions from Native Code
Synchronization
Overview of the Garbage Collection Architecture