Java Reference
In-Depth Information
22.3.4 Create the native implementation
Our fourth step is to create the native implementation. From the generated header
file we now know the required signature of the native implementation function.
Our implementation must include the header file, and we must provide native
function definitions that exactly match the signatures in the header file. Let's
implement the non-static method first. Recall that the function signature, obtained
from the generated header file, is
JNIEXPORT void JNICALL
Java - javatech - jni22 - JNIHelloWorld - nativeHelloWorld (
JNIEnv *, jobject);
ACimplementation of the method is quite simple. We just use the C stdio
library method printf to print “Hello World” on standard output. Actually, we
prefix the string with a few spaces and the word “< native > to distinguish
this output from the Java System.out.println statements. We also include
the phrase (non-static) to distinguish this output from the output to be
generated later by the static method.
#include "javatech - jni22 - JNIHelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL
Java - javatech - jni22 - JNIHelloWorld - nativeHelloWorld (
JNIEnv *, jobject)
{
printf ( "
< native > Hello World (non-static) \ n " );
return;
}
The first line #includes the generated header file, as is required.
The static method implementation is very similar to the non-static method.
Since we don't do anything with either the jclass or jobject input parameters,
the only difference is the function signature. We can put this function definition
in the same C/C
++
file as the previous function.
JNIEXPORT void JNICALL
Java - javatech - jni22 - JNIHelloWorld - nativeHelloWorldStatic (
JNIEnv *, jclass)
{
printf ("
<native> Hello World (static)\n");
return;
}
Search WWH ::




Custom Search