|
|
Step By Step |
Now, you can finally write the implementation for the native method in a language other than the Java programming language.The function that you write must have the same function signature as the one generated by
javahin theHelloWorld.hfile in Step 3: Create the .h File. Recall that the function signature generated for theHelloWorldclass'sdisplayHelloWorld()native method looks like this:Here's the C language implementation for the native methodJNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *, jobject);Java_HelloWorld_displayHelloWorld(). This implementation is in the file namedHelloWorldImp.c.The implementation for#include <jni.h> #include "HelloWorld.h" #include <stdio.h> JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) { printf("Hello world!\n"); return; }Java_HelloWorld_displayHelloWorld()is straightforward. The function uses theprintf()function to display the string "Hello World!" and then returns.This file includes three header files:
- 1.
jni.h- This header file provides information that the native language code requires to interact with the Java runtime system. When writing native methods, you must always include this file in your native language source files.- 2.
HelloWorld.h- The.hfile that you generated in Step 3: Create the .h File.- 3.
stdio.h- The code snippet above includesstdio.hbecause it uses theprintf()function. Theprintf()function is part of thestdio.hlibrary.
|
|
Step By Step |