Game Development Reference
In-Depth Information
return (*env)->GetArrayLength(env, jarray);
}
/**
* Library main sub
*/
int lib_main(int argc, char **argv)
{
int i;
jni_printf("Entering LIB MAIN");
for ( i = 0 ; i < argc ; i++ ) {
jni_printf("Lib Main argv[%d]=%s", i, argv[i]);
}
return 0;
}
Let's dissect this file to understand what it does. Any C/C++ program that plans to do JNI calls must
include the header file:
#include <jni.h>
This header file has the prototypes for all the JNI system calls to be used by your library. It can be
found in your system's Java home under
JAVA_HOME/include
, with extra Linux dependencies under
JAVA_HOME/include/linux
. At compile time, these paths must be included using
-I$JAVA_HOME/include
and
-I$JAVA_HOME/include/linux
in the
Makefile
(The
agcc
script you created in Chapter 1 will take care
of all this).
Next, it includes the
jni_Natives
header file:
#include "include/jni_Natives.h"
This file contains the user-defined JNI prototypes for all native methods defined in the
jni.Natives
class. It is machine-generated and must not be edited by the user. The actual generation will be set up in
the Makefile. To generate this file manually, the following command can be used:
javah -cp ../bin -d include jni.Natives
Here,
javah
is the Java Virtual Machine (JVM) command to generate native header files from Java
classes,
-cp
defines the class path search path,
-d
include tells
javah
to save the file in the include folder
(creating it if required), and
jni.Natives
is the Java class name from which you wish to extract the
headers.
Next, the following constants are defined:
#define CB_CLASS "jni/Natives"
#define CB_CLASS_MSG_CB "OnMessage"
#define CB_CLASS_MSG_SIG "(Ljava/lang/String;I)V"
CB_CLASS
is the name of the Java class that will be invoked within C (note that the period separating
path names is replaced by
/
).
CB_CLASS_MSG_CB
is the name of the Java method (
OnMessage
) that will be