Creating a C Header File Using javahIn the last step, we created a Java file which simply declared a native method and compiled it. When the method is invoked at runtime, how will the virtual machine find it? How will it know what function to invoke?
JNI C function names are generated by mangling the Java method's name, declaring class, and signature in a way to make sure
that every Java method has a distinct C function name. The
Let's run javah -classpath [wherever you compiled HelloWorld] -o HelloWorld.h example.jni.HelloWorld
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class example_jni_HelloWorld */
#ifndef _Included_example_jni_HelloWorld
#define _Included_example_jni_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: example_jni_HelloWorld
* Method: writeHelloWorldToStdout
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_example_jni_HelloWorld_writeHelloWorldToStdout
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
So there is our C function prototype. Note that the compiler has examined the Java native method's return type and decided our
C function should be a
static, the second argument would be a jobject representing
the Java this reference, rather than the jclass.
The We're ready to write the function's implementation. |