Game Development Reference
In-Depth Information
JNI Cheat Sheet
Here you'll find a very helpful JNI cheat sheet that you can use when working on any type of
JNI/C-related project. This section is divided in two basic communication pipelines:
Native calls within Java code : These are used to invoke any C function
within Java code.
C to Java callbacks : These are useful to tell Java about events that
occur on the native side, such as audio/video initialization and so forth.
JNI Method Syntax
When implementing native methods, the syntax on the C implementation must be
JNIEXPORT <RETURNTYPE> JNICALL Java_<PACKAGE>_<CLASS>_<METHOD>
(JNIEnv * env, jclass cls, <ARGUMENTS>)
Note that any periods used in the Java names must be replaced with underscore characters.
Thus the Java native method of
package native;
class Natives {
public static native void GameMain(String[] args);
}
must be declared in C as
JNIEXPORT void JNICALL Java_native_Natives_GameMain
(JNIEnv * env, jclass cls, jobjectArray jargv)
Take a look at the arguments. They are as follows:
env : This is a C pointer to the JNI environment. It can be used to perform
miscellaneous Java tasks within C such as:
Load classes, methods, exceptions, and more.
Invoke Java methods or exceptions.
cls : This is an opaque reference to the class that is invoking the C
function. It can be used to extract member variables, invoke methods or
raise exceptions within the calling class.
jargv : This is the most useful argument. In this case it represents the
user defined string array sent by the caller (in this particular example,
the startup arguments sent to the native layer). Note that this is a
JNI object array, which is an opaque type representing a Java string
array. It must be converted into a C array, which is shown in the
next section.
 
Search WWH ::




Custom Search