|
|
Java Native Interface Programming |
This lesson illustrates how to declare a native method in the Java programming language and how to generate the corresponding C/C++ language function prototype.The Java Side
Our first example,
Prompt.java, contains a native method that accepts and prints a Java programming language string. The program calls the native method, which waits for user input and then returns the line the user typed in.The
Promptclass contains amainmethod which is used to invoke the program. In addition, there is agetLinenative method:private native String getLine(String prompt);Notice that the declarations for native methods are almost identical to the declarations for regular, non-native Java programming language methods. There are two differences. First, native methods must have the
nativekeyword. Thenativekeyword informs the Java programming language compiler that the implementation for this method is provided in another language. Secondly, the native method declaration is terminated with a semicolon (the statement terminator symbol) because there are no implementations for native methods in the Java programming language class file.The Native Language Side
You must declare and implement native methods in a native language, such as C or C++. Before you do this, it is helpful to generate the header file that contains the function prototype for the native method implementation.Compile the
Prompt.javafile and then generate the.hfile. First, compile thePrompt.javafile as follows:javac Prompt.javaOnce you have successfully compiled
Prompt.javaand have created thePrompt.classfile, you can generate a JNI-style header file by specifying a-jnioption tojavah:javah -jni PromptExamine the Prompt.h file. Note the function prototype for the native method
getLinethat you declared in Prompt.java.JNIEXPORT jstring JNICALL Java_Prompt_getLine(JNIEnv *, jobject, jstring);The native method function definition in the implementation code must match the generated function prototype in the header file. Always include
JNIEXPORTandJNICALLin your native method function prototypes.JNIEXPORTandJNICALLensure that the source code compiles on platforms such as Win32 that require special keywords for functions exported from dynamic link libraries.Native method names are concatenated from the following components:
- the prefix
Java_- the fully qualified class name
- an underscore "_" separator
- the method name
Graphically, this looks as follows:
Note: Overloaded native method names, in addition to the above components, have an extra two underscores "__" appended to the method name followed by the argument signature.
Thus, the native code implementation for the
Prompt.getLinemethod becomesJava_Prompt_getLine. (There is no package name component because thePromptclass is in the default package.)Each native method has two parameters in addition to those parameters that you declared on the Java programming language side. The first parameter,
JNIEnv *, is the JNI interface pointer. This interface pointer is organized as a function table, with every JNI function at a known table entry point. Your native method invokes specific JNI functions to access Java objects through theJNIEnv *pointer. Thejobjectparameter is a reference to the object itself (it is like thethispointer in C++).Lastly, notice that the JNI has a set of type names, such as
jobjectandjstring, and each type corresponds to Java programming language types. This is covered in the next section.
|
|
Java Native Interface Programming |