The C Header File

As can be seen from the Java wrapper classes we need seven JNI functions - that is we need two for the contructors and five for the methods.

Tip: All functions in the wrapper class with native before it needs to be defined in the JNI(C) files.

The function name of the C function that gets mapped to a Java method is made up as follows:

JNIEXPORT jnitype JNICALL Java_<classname>_<methodname>(JNIEnv * , jobject , <...> );

where <classname> denotes the fully qualified classname (eg. gtk.GtkButton will become gtk_GtkButton) and <methodname> denotes the name of the method (eg. nativenew will still be nativenew) <...> denote any other parameters that the method needs (if there is any). [1]

You can use Table 2-1 and Table 2-2 to get from Java types to JNI types. Please note that Integer etc. should be accessed through a jobject.

Table 2-1. Java to JNI conversions

Basic TypeJNI Type
booleanjboolean
bytejbyte
charjchar
shortjshort
intjint
longjlong
longjlong
floatjfloat
voidvoid

Table 2-2. Java to JNI conversions

TypeJNI Type
Stringjstring
Objectjobject

For instance the definitions for the two constructors will be look like follows:

JNIEXPORT jlong JNICALL Java_gtk_GtkButton_nativenew(JNIEnv * , jobject );

JNIEXPORT jlong JNICALL Java_gtk_GtkButton_nativenewWithLabel(JNIEnv * , jobject , jstring );

We have to include the jni.h header file (and of course the gtk.h file) so the C header file will look something like Example 2-3.

Example 2-3. gtk_GtkButton.h

#include <jni.h>
#include <gtk/gtk.h>

#ifndef _Included_gtk_GtkButton
#define _Included_gtk_GtkButton
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jlong JNICALL Java_gtk_GtkButton_nativenew (JNIEnv *, jobject);

JNIEXPORT jlong JNICALL Java_gtk_GtkButton_nativenewWithLabel (JNIEnv *, jobject, jstring);

JNIEXPORT void JNICALL Java_gtk_GtkButton_pressed (JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_gtk_GtkButton_released (JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_gtk_GtkButton_clicked (JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_gtk_GtkButton_enter (JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_gtk_GtkButton_leave (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Notes

[1]

For more information on the "translation" between Java methods and JNI functions refer to Javasoft's documentation or tutorials.