Integrating Java and Native Programs |
This section illustrates how to declare a native method in Java and how to generate the corresponding C/C++ function prototype.The Java Side
Our first example,
Prompt.java
, contains a native method that accepts and prints a Java string. The program calls the native method, which waits for user input and then returns the line the user typed in.The
Prompt
class contains amain
method, which is used to invoke the program, and a native method namedgetLine
, which is declared as follows:private native String getLine(String prompt);Notice that the declarations for native methods are almost identical to the declarations for regular, non-native Java methods. However, you declare native methods differently, as follows:
- First, native methods must have the
native
keyword. Thenative
keyword informs the Java 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 the Java class file does not include implementations for native methods.
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.java
file and then generate the.h
file. First, compile thePrompt.java
file as follows:javac Prompt.javaOnce you have successfully compiled
Prompt.java
and have created thePrompt.class
file, you can generate a JNI-style header file by specifying a-jni
option tojavah
:javah -jni PromptExamine the
Prompt.h
file. Note the function prototype for the native methodgetLine
that you declared inPrompt.java
.JNIEXPORT jstring JNICALL Java_Prompt_getLine(JNIEnv *, jobject, jstring);The native method function definition in the implementation code must match the generated function signature in the header file. Always include
JNIEXPORT
andJNICALL
in your native method function signatures.JNIEXPORT
andJNICALL
ensure 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:
Thus, the native code implementation for the
Prompt.getLine
method becomesJava_Prompt_getLine
. (Remember that no package name component appears because thePrompt
class is in the default package.)
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. To illustrate, we created a second version of the Java program,Prompt2.java
, and overloaded thegetLine
method by adding a second argument of typeint
. The twogetLine
method names in thePrompt2.h
header file look as follows:Java_Prompt2_getLine__Ljava_lang_String_2 Java_Prompt2_getLine__Ljava_lang_String_2I
Recall from Step 3: Create the .h File, each native method has two parameters in addition to those parameters that you declared on the Java 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. Thejobject
parameter is a reference to the object itself (it is like thethis
pointer in Java).Lastly, notice that the JNI has a set of type names, such as
jobject
andjstring
, and each type corresponds to Java types. This is covered in the next section.
Integrating Java and Native Programs |