Generating the sample adapter
To generate the sample adapter, run the following commands:
1. javac Printfile.java
This compiles the Java class.
2. javah -jni PrintFile
This generates the header file, PrintFile.h, displayed below.
JNIEXPORT jint JNICALL
Java_PrintFile_printFile__Ljava_lang_String_2Ljava_lang_String_2
(JNIEnv *, jobject, jstring, jstring);
JNIEXPORT jint JNICALL
Java_PrintFile_printFile__Ljava_lang_String_2(JNIEnv *, jobject, jstring);
3. Implement the native methods. An outline is shown below.
JNIEXPORT jint JNICALL
Java_PrintFile_printFile__Ljava_lang_String_2
(JNIEnv *env, jobject obj, jstring strFilename)
{ jboolean isCopy;
const char *filename = (*env)->GetStringUTFChars(env,
strFilename, &isCopy);
...
C code to actually print the file
...
if (isCopy == JNI_TRUE)
(*env)->ReleaseStringUTFChars(env, strFilename, filename);
return iSuccess;
}