Contents | Prev | Next

JNI Functions


4

This chapter serves as the reference section for the JNI functions. It provides a complete listing of all the JNI functions. It also presents the exact layout of the interface function table, which is the means for accessing native methods through the JNI.

A portion of this chapter is adapted from Netscape's JRI documentation.

The reference material groups functions by their usage. The reference section is organized by the following functional areas:
Version Information page 4-30
Class Operations page 4-30
Exceptions page 4-32
Global and Local References page 4-34
Object Operations page 4-35
Accessing Fields of Objects page 4-39
Calling Instance Methods page 4-42
Accessing Static Fields page 4-51
Calling Static Methods page 4-54
String Operations page 4-58
Array Operations page 4-62
Registering Native Methods page 4-70
Monitor Operations page 4-71
Java VM Interface page 4-72

Interface Function Table

Each function is accessible at a fixed offset through the JNIEnv argument. The JNIEnv type is a pointer to a structure storing all JNI function pointers. It is defined as follows:
typedef const struct JNINativeInterface *JNIEnv;

The VM initializes the function table, as shown by Code Example 4-1. Note that the first three entries are reserved for future compatibility with COM. In addition, we reserve a number of additional NULL entries near the beginning of the function table, so that, for example, a future class-related JNI operation can be added after FindClass, rather than at the end of the table.

Note that the function table can be shared among all JNI interface pointers.

Code Example 4-1 Interface Function Table

const struct JNINativeInterface ... = {
    NULL,
    NULL,
    NULL,
    NULL,
    GetVersion,

    DefineClass,
    FindClass,
    NULL,
    NULL,
    NULL,
    GetSuperclass,
    IsSubclassOf,
    NULL,

    Throw,
    ThrowNew,
    ExceptionOccurred,
    ExceptionDescribe,
    ExceptionClear,
    FatalError,
    NULL,
    NULL,

    NewGlobalRef,
    DeleteGlobalRef,
    DeleteLocalRef,
    IsSameObject,
    NULL,
    NULL,

    AllocObject,
    NewObject,
    NewObjectV,
    NewObjectA,

    GetObjectClass,
    IsInstanceOf,

    GetMethodID,

    CallObjectMethod,
    CallObjectMethodV,
    CallObjectMethodA,
    CallBooleanMethod,
    CallBooleanMethodV,
    CallBooleanMethodA,
    CallByteMethod,
    CallByteMethodV,
    CallByteMethodA,
    CallCharMethod,
    CallCharMethodV,
    CallCharMethodA,
    CallShortMethod,
    CallShortMethodV,
    CallShortMethodA,
    CallIntMethod,
    CallIntMethodV,
    CallIntMethodA,
    CallLongMethod,
    CallLongMethodV,
    CallLongMethodA,
    CallFloatMethod,
    CallFloatMethodV,
    CallFloatMethodA,
    CallDoubleMethod,
    CallDoubleMethodV,
    CallDoubleMethodA,
    CallVoidMethod,
    CallVoidMethodV,
    CallVoidMethodA,

    CallNonvirtualObjectMethod,
    CallNonvirtualObjectMethodV,
    CallNonvirtualObjectMethodA,
    CallNonvirtualBooleanMethod,
    CallNonvirtualBooleanMethodV,
    CallNonvirtualBooleanMethodA,
    CallNonvirtualByteMethod,
    CallNonvirtualByteMethodV,
    CallNonvirtualByteMethodA,
    CallNonvirtualCharMethod,
    CallNonvirtualCharMethodV,
    CallNonvirtualCharMethodA,
    CallNonvirtualShortMethod,
    CallNonvirtualShortMethodV,
    CallNonvirtualShortMethodA,
    CallNonvirtualIntMethod,
    CallNonvirtualIntMethodV,
    CallNonvirtualIntMethodA,
    CallNonvirtualLongMethod,
    CallNonvirtualLongMethodV,
    CallNonvirtualLongMethodA,
    CallNonvirtualFloatMethod,
    CallNonvirtualFloatMethodV,
    CallNonvirtualFloatMethodA,
    CallNonvirtualDoubleMethod,
    CallNonvirtualDoubleMethodV,
    CallNonvirtualDoubleMethodA,
    CallNonvirtualVoidMethod,
    CallNonvirtualVoidMethodV,
    CallNonvirtualVoidMethodA,

    GetFieldID,

    GetObjectField,
    GetBooleanField,
    GetByteField,
    GetCharField,
    GetShortField,
    GetIntField,
    GetLongField,
    GetFloatField,
    GetDoubleField,
    SetObjectField,
    SetBooleanField,
    SetByteField,
    SetCharField,
    SetShortField,
    SetIntField,
    SetLongField,
    SetFloatField,
    SetDoubleField,

    GetStaticMethodID,

    CallStaticObjectMethod,
    CallStaticObjectMethodV,
    CallStaticObjectMethodA,
    CallStaticBooleanMethod,
    CallStaticBooleanMethodV,
    CallStaticBooleanMethodA,
    CallStaticByteMethod,
    CallStaticByteMethodV,
    CallStaticByteMethodA,
    CallStaticCharMethod,
    CallStaticCharMethodV,
    CallStaticCharMethodA,
    CallStaticShortMethod,
    CallStaticShortMethodV,
    CallStaticShortMethodA,
    CallStaticIntMethod,
    CallStaticIntMethodV,
    CallStaticIntMethodA,
    CallStaticLongMethod,
    CallStaticLongMethodV,
    CallStaticLongMethodA,
    CallStaticFloatMethod,
    CallStaticFloatMethodV,
    CallStaticFloatMethodA,
    CallStaticDoubleMethod,
    CallStaticDoubleMethodV,
    CallStaticDoubleMethodA,
    CallStaticVoidMethod,
    CallStaticVoidMethodV,
    CallStaticVoidMethodA,

    GetStaticFieldID,

    GetStaticObjectField,
    GetStaticBooleanField,
    GetStaticByteField,
    GetStaticCharField,
    GetStaticShortField,
    GetStaticIntField,
    GetStaticLongField,
    GetStaticFloatField,
    GetStaticDoubleField,

    SetStaticObjectField,
    SetStaticBooleanField,
    SetStaticByteField,
    SetStaticCharField,
    SetStaticShortField,
    SetStaticIntField,
    SetStaticLongField,
    SetStaticFloatField,
    SetStaticDoubleField,

    NewString,
    GetStringLength,
    GetStringChars,
    ReleaseStringChars,

    NewStringUTF,
    GetStringUTFLength,
    GetStringUTFChars,
    ReleaseStringUTFChars,

    GetArrayLength,
 
    NewObjectArray,
    GetObjectArrayElement,
    SetObjectArrayElement,

    NewBooleanArray,
    NewByteArray,
    NewCharArray,
    NewShortArray,
    NewIntArray,
    NewLongArray,
    NewFloatArray,
    NewDoubleArray,

    GetBooleanArrayElements,
    GetByteArrayElements,
    GetCharArrayElements,
    GetShortArrayElements,
    GetIntArrayElements,
    GetLongArrayElements,
    GetFloatArrayElements,
    GetDoubleArrayElements,

    ReleaseBooleanArrayElements,
    ReleaseByteArrayElements,
    ReleaseCharArrayElements,
    ReleaseShortArrayElements,
    ReleaseIntArrayElements,
    ReleaseLongArrayElements,
    ReleaseFloatArrayElements,
    ReleaseDoubleArrayElements,

    GetBooleanArrayRegion,
    GetByteArrayRegion,
    GetCharArrayRegion,
    GetShortArrayRegion,
    GetIntArrayRegion,
    GetLongArrayRegion,
    GetFloatArrayRegion,
    GetDoubleArrayRegion,
    SetBooleanArrayRegion,
    SetByteArrayRegion,
    SetCharArrayRegion,
    SetShortArrayRegion,
    SetIntArrayRegion,
    SetLongArrayRegion,
    SetFloatArrayRegion,
    SetDoubleArrayRegion,

    RegisterNatives,
    UnregisterNatives,

    MonitorEnter,
    MonitorExit,

    GetJavaVM,
};

Version Information

GetVersion

jint GetVersion(JNIEnv *env);

Returns the version of the native method interface.

PARAMETERS:

env: the JNI interface pointer.

RETURNS:

Returns the major version number in the higher 16 bits and the minor version number in the lower 16 bits.

In JDK1.1, GetVersion() returns 0x00010001.

Class Operations

DefineClass

jclass DefineClass(JNIEnv *env, jobject loader,
const char *buf, jsize bufLen);

Defines a class from a buffer of raw class data.

PARAMETERS:

env: the JNI interface pointer.

loader: a class loader assigned to the defined class.

buf: buffer containing the .class file data.

bufLen: buffer length.

RETURNS:

Returns a Java class object or NULL if an error occurs.

THROWS:

ClassFormatError: if the class data does not specify a valid class.

FindClass

jclass FindClass(JNIEnv *env, const char *name);

This function loads a locally defined class. It searches the directories and zip files specified by the CLASSPATH environment variable for the class with the specified name.

PARAMETERS:

env: the JNI interface pointer.

name: a fully qualified class name (that is, a package name, delimited by "/", followed by the class name). If the name begins with "[" (the array signature character), it returns an array class.

RETURNS:

Returns a class object from a fully qualified name, or NULL if the class cannot be found.

GetSuperclass

jclass GetSuperclass(JNIEnv *env, jclass clazz);

If clazz represents any class other than the class Object, then this function returns the object that represents the superclass of the class specified by clazz.

If clazz specifies the class Object or clazz represents an interface, this function returns NULL.

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

RETURNS:

Returns the super class of the class represented by clazz, or NULL.

IsSubclassOf

jboolean IsSubclassOf(JNIEnv *env, jclass clazz1,
jclass clazz2);

Determines whether an object of clazz1 can be safely cast to clazz2.

PARAMETERS:

env: the JNI interface pointer.

clazz1: the first class argument.

clazz2: the second class argument.

RETURNS:

Returns JNI_TRUE if either of the following is true:

· The first and second class arguments refer to the same Java class.

· The first class is a subclass of the second class.

· The first class has the second class as one of its interfaces.

Exceptions

Throw

jint Throw(JNIEnv *env, jobject obj);

Causes a java.lang.Throwable object to be thrown.

PARAMETERS:

env: the JNI interface pointer.

obj: a java.lang.Throwable object.

RETURNS:

Returns zero on success; a negative value on failure.

ThrowNew

jint ThrowNew(JNIEnv *env, jclass clazz,
const char *message);

Constructs an exception object from the specified class with the message specified by message and causes that exception to be thrown.

PARAMETERS:

env: the JNI interface pointer.

clazz: a subclass of java.lang.Throwable.

message: the message used to construct the java.lang.Throwable object.

RETURNS:

Returns zero on success; a negative value on failure.

ExceptionOccurred

jobject ExceptionOccurred(JNIEnv *env);

Determines if an exception is being thrown. The exception stays being thrown until either the native code calls ExceptionClear(), or the Java code handles the exception.

PARAMETERS:

env: the JNI interface pointer.

RETURNS:

Returns the exception object that is currently in the process of being thrown, or NULL if no exception is currently being thrown.

ExceptionDescribe

void ExceptionDescribe(JNIEnv *env);

Prints an exception and a backtrace of the stack to a system error reporting channel, such as stderr. This is a convenience routine provided for debugging.

PARAMETERS:

env: the JNI interface pointer.

ExceptionClear

void ExceptionClear(JNIEnv *env);

Clears any exception that is currently being thrown. If no exception is currently being thrown, this routine has no effect.

PARAMETERS:

env: the JNI interface pointer.

FatalError

void FatalError(JNIEnv *env, char * msg);

Raises a fatal error and does not expect the VM to recover.

PARAMETERS:

env: the JNI interface pointer.

msg: an error message.

Global and Local References

NewGlobalRef

jref NewGlobalRef(JNIEnv *env, jref ref);

Creates a new global reference to the object referred to by the ref argument. The ref argument may be a global or local reference. Global references must be explicitly disposed of by calling DeleteGlobalRef().

PARAMETERS:

env: the JNI interface pointer.

ref: a global or local reference.

RETURNS:

Returns a global reference, or NULL, if the system runs out of memory.

DeleteGlobalRef

void DeleteGlobalRef(JNIEnv *env, jref *globalRef);

Deletes the global reference pointed to by globalRef and sets *globalRef to NULL.

PARAMETERS:

env: the JNI interface pointer.

globalRef: a global reference.

DeleteLocalRef

void DeleteLocalRef(JNIEnv *env, jref *localRef);

Deletes the local reference pointed to by localRef and sets *localRef to NULL.

PARAMETERS:

env: the JNI interface pointer.

localRef: a local reference.

Object Operations

AllocObject

jobject AllocObject(JNIEnv *env, jclass clazz);

Allocates a new Java object without invoking any of the constructors for the object. Returns a reference to the object.

The clazz argument must not refer to an array class.

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

RETURNS:

Returns a Java object, or NULL if the object cannot be constructed.

THROWS:

InstantiationException: if the class is an interface or an abstract class.

NewObject

jobject NewObject(JNIEnv *env, jclass clazz,
jmethodID methodID, ...);

Constructs a new Java object. The method ID indicates which constructor method to invoke. This ID must be obtained by calling GetMethodID() with <init> as the method name and void (V) as the return type.

Programmers place all arguments that are to be passed to the constructor immediately following the methodID argument. NewObject() accepts these arguments and passes them to the Java method that the programmer wishes to invoke.

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

methodID: the method ID of the constructor.

RETURNS:

Returns a Java object, or NULL if the object cannot be constructed.

THROWS:

InstantiationException: if the class is an interface or an abstract class.

NewObjectA

jobject NewObjectA(JNIEnv *env, jclass clazz,
jmethodID methodID, jvalue *args);

Constructs a new Java object. The method ID indicates which constructor method to invoke. This ID must be obtained by calling GetMethodID() with <init> as the method name and void (V) as the return type.

Programmers place all arguments that are to be passed to the constructor in an args array of jvalues that immediately follows the methodID argument. NewObjectA() accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

methodID: the method ID of the constructor.

args: an array of arguments to the constructor.

RETURNS:

Returns a Java object, or NULL if the object cannot be constructed.

THROWS:

InstantiationException: if the class is an interface or an abstract class.

NewObjectV

jobject NewObjectV(JNIEnv *env, jclass clazz,
jmethodID methodID, va_list args);

Constructs a new Java object. The method ID indicates which constructor method to invoke. This ID must be obtained by calling GetMethodID() with <init> as the method name and void (V) as the return type.

Programmers place all arguments that are to be passed to the constructor in an args argument of type va_list that immediately follows the methodID argument. NewObjectV() accepts these arguments, and, in turn, passes them to the Java method that the programmer wishes to invoke.

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

methodID: the method ID of the constructor.

args: a va_list of arguments to the constructor.

RETURNS:

Returns a Java object, or NULL if the object cannot be constructed.

THROWS:

InstantiationException: if the class is an interface or an abstract class.

GetObjectClass

jclass GetObjectClass(JNIEnv *env, jobject obj);

Returns the class of an object.

PARAMETERS:

env: the JNI interface pointer.

obj: a Java object (must not be NULL).

RETURNS:

Returns a Java class object.

IsInstanceOf

jboolean IsInstanceOf(JNIEnv *env, jobject obj,
jclass clazz);

Tests whether an object is an instance of a class.

PARAMETERS:

env: the JNI interface pointer.

obj: a Java object.

clazz: a Java class object.

RETURNS:

Returns JNI_TRUE if obj can be cast to clazz; otherwise, returns JNI_FALSE. A NULL object can be cast to any class.

IsSameObject

jboolean IsSameObject(JNIEnv *env, jobject ref1,
jobject ref2);

Tests whether two references refer to the same Java object.

PARAMETERS:

env: the JNI interface pointer.

ref1: a Java object.

ref2: a Java object.

RETURNS:

Returns JNI_TRUE if ref1 and ref2 refer to the same Java object, or are both NULL; otherwise, returns JNI_FALSE.

Accessing Fields of Objects

GetFieldID

jfieldID GetFieldID(JNIEnv *env, jclass clazz,
const char *name, const char *sig);

Returns the field ID for an instance (non-static) field of a class. The field is specified by its name and signature. The GetField and SetField families of accessor functions use field IDs to retrieve object fields.

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

name: the field name in a 0-terminated UTF-8 string.

sig: the field signature in a 0-terminated UTF-8 string.

RETURNS:

Returns a field ID, or NULL if the operation fails.

THROWS:

NoSuchFieldError: if the specified field cannot be found.

GetField

NativeType GetField(JNIEnv *env, jobject obj,
jfieldID fieldID);

This family of accessor routines returns the value of an instance (non-static) field of an object. The field to access is specified by a field ID obtained by calling GetFieldID().

Table 4-1 describes the GetField routine name and result type. Note that GetField is not a real function name, nor is NativeType a real type name. You should replace GetField with one of the actual routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-1 GetField Family of Accessor Routines

GetField Routine Name Native Type Java Type
GetObjectField() jobject Object
GetBooleanField() jboolean boolean
GetByteField() jbyte byte
GetCharField() jchar char
GetShortField() jshort short
GetIntField() jint int
GetLongField() jlong long
GetFloatField() jfloat float
GetDoubleField() jdouble double

PARAMETERS:

env: the JNI interface pointer.

obj: a Java object (must not be NULL).

fieldID: a valid field ID.

RETURNS:

Returns the content of the field.

SetField

void SetField(JNIEnv *env, jobject obj, jfieldID fieldID,
NativeType value);

This family of accessor routines sets the value of an instance (non-static) field of an object. The field to access is specified by a field ID obtained by calling GetFieldID().

Table 4-2 describes the SetField routine name and value type. Note that SetField is not a real function name, nor is NativeType a real type name. You should replace SetField with one of the actual routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-2 SetField Family of Accessor Routines

SetField Routine Native Type Java Type
SetObjectField() jobject Object
SetBooleanField() jboolean boolean
SetByteField() jbyte byte
SetCharField() jchar char
SetShortField() jshort short
SetIntField() jint int
SetLongField() jlong long
SetFloatField() jfloat float
SetDoubleField() jdouble double

PARAMETERS:

env: the JNI interface pointer.

obj: a Java object (must not be NULL).

fieldID: a valid field ID.

value: the new value of the field.

Calling Instance Methods

GetMethodID

jmethodID GetMethodID(JNIEnv *env, jclass clazz,
const char *name, const char *sig);

Returns the method ID for an instance (non-static) method of a class or interface. The method may be defined in one of the clazz's super classes and inherited by clazz. The method is determined by its name and signature.

To obtain the method ID of a constructor, supply <init> as the method name and void (V) as the return type.

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

name: the method name in a 0-terminated UTF-8 string.

sig: the method signature in 0-terminated UTF-8 string.

RETURNS:

Returns a method ID, or NULL if the specified method cannot be found.

THROWS:

NoSuchMethodError: if the specified method cannot be found.

CallMethod

NativeType CallMethod(JNIEnv *env, jobject obj,
jmethodID methodID, ...);

This family of operations invokes an instance (non-static) method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetMethodID().

When this function is used to call private methods and constructors, the method ID must be derived from the real class of obj, not from one of its super classes.

Programmers place all arguments that are to be passed to the method immediately following the methodID argument. The CallMethod routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke.

Table 4-3 describes each of the method calling routines according to their result type. Note that CallMethod is not a real function name, nor is NativeType a real type name. You should replace CallMethod with one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-3 CallMethod Family of Method Calling Routines

CallMethod Routine Name Native Type Java Type
CallVoidMethod() void void
CallObjectMethod() jobject Object
CallBooleanMethod() jboolean boolean
CallByteMethod() jbyte byte
CallCharMethod() jchar char
CallShortMethod() jshort short
CallIntMethod() jint int
CallLongMethod() jlong long
CallFloatMethod() jfloat float
CallDoubleMethod() jdouble double

PARAMETERS:

env: the JNI interface pointer.

obj: a Java object.

methodID: a method ID.

RETURNS:

Returns the result of calling the Java method.

CallMethodA

NativeType CallMethodA(JNIEnv *env, jobject obj,
jmethodID methodID, jvalue *args);

This family of operations invokes an instance (non-static) method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetMethodID().

When this function is used to call private methods and constructors, the method ID must be derived from the real class of obj, not from one of its super classes.

Programmers place all arguments to the method in an args array of jvalues that immediately follows the methodID argument. The CallMethodA routine accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.

Table 4-4 describes each of the CallMethodA method calling routines according to their result types. Note that CallMethodA is not a real function name, nor is NativeType a real type name. You should replace CallMethodA with one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-4 CallMethodA Family of Method Calling Routines

CallMethodA Routine Name Native Type Java Type
CallVoidMethodA() void void
CallObjectMethodA() jobject Object
CallBooleanMethodA() jboolean boolean
CallByteMethodA() jbyte byte
CallCharMethodA() jchar char
CallShortMethodA() jshort short
CallIntMethodA() jint int
CallLongMethodA() jlong long
CallFloatMethodA() jfloat float
CallDoubleMethodA() jdouble double

PARAMETERS:

env: the JNI interface pointer.

obj: a Java object.

methodID: a method ID.

args: an array of arguments.

RETURNS:

Returns the result of calling the Java method.

CallMethodV

NativeType CallMethodV(JNIEnv *env, jobject obj,
jmethodID methodID, va_list args);

This family of operations invokes an instance (non-static) method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetMethodID().

When this function is used to call private methods and constructors, the method ID must be derived from the real class of obj, not from one of its super classes.

Programmers place all arguments to the method in an args argument of type va_list that immediately follows the methodID argument. The CallMethodV routine accepts the arguments, and, in turn, passes them to the Java method that the programmer wishes to invoke.

Table 4-5 describes each of the CallMethodV method calling routines according to their result types. Note that CallMethodV is not a real function name, nor is NativeType a real type name. You should replace CallMethodV with one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-5 CallMethodV Family of Method Calling Routines

CallMethodV Routine Name Native Type Java Type
CallVoidMethodV() void void
CallObjectMethodV() jobject Object
CallBooleanMethodV() jboolean boolean
CallByteMethodV() jbyte byte
CallCharMethodV() jchar char
CallShortMethodV() jshort short
CallIntMethodV() jint int
CallLongMethodV() jlong long
CallFloatMethodV() jfloat float
CallDoubleMethodV() jdouble double

PARAMETERS:

env: the JNI interface pointer.

obj: a Java class object.

methodID: a method ID.

args: a va_list of arguments.

RETURNS:

Returns the result of calling the Java method.

CallNonvirtualMethod

NativeType CallNonvirtualMethod(JNIEnv *env, jobject obj,
jclass clazz, jmethodID methodID, ...);

This family of operations invokes an instance (non-static) method on a Java object, according to the specified class and method ID. The methodID argument must be obtained by calling GetMethodID() on the class clazz.

The CallNonvirtualMethod and CallMethod families of routines are different. The CallMethod routines invoke the method based on the class of the object, whereas the CallNonvirtualMethod routines invoke the method based on the class, designated by the clazz parameter, from which the method ID is obtained. The method ID must be obtained from the real class of the object or from one of its superclasses.

Programmers place all arguments that are to be passed to the method immediately following the methodID argument. The CallNonvirtualMethod routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke.

Table 4-6 describes each of the method calling routines according to their result type. Note that CallNonvirtualMethod is not a real function name, nor is NativeType a real type name. You should replace CallNonvirtualMethod with one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-6 CallNonvirtualMethod Family of Routines

CallNonvirtualMethod Routine Name Native Type Java Type
CallNonvirtualVoidMethod() void void
CallNonvirtualObjectMethod() jobject Object
CallNonvirtualBooleanMethod() jboolean boolean
CallNonvirtualByteMethod() jbyte byte
CallNonvirtualCharMethod() jchar char
CallNonvirtualShortMethod() jshort short
CallNonvirtualIntMethod() jint int
CallNonvirtualLongMethod() jlong long
CallNonvirtualFloatMethod() jfloat float
CallNonvirtualDoubleMethod() jdouble double

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class.

obj: a Java object.

methodID: a method ID.

RETURNS:

Returns the result of calling the Java method.

CallNonvirtualMethodA

NativeType CallNonvirtualMethodA(JNIEnv *env, jobject obj,
jclass clazz, jmethodID methodID, jvalue *args);

This family of operations invokes an instance (non-static) method on a Java object, according to the specified class and method ID. The methodID argument must be obtained by calling GetMethodID() on the class clazz.

The CallNonvirtualMethodA family of routines and the CallMethodA family of routines are different. CallMethodA routines invoke the method based on the class of the object, while CallNonvirtualMethodA routines invoke the method based on the class, designated by the clazz parameter, from which the method ID is obtained. The method ID must be obtained from the real class of the object or from one of its superclasses.

Programmers place all arguments to the method in an args array of jvalues that immediately follows the methodID argument. The CallNonvirtualMethodA routine accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.

Table 4-7 describes each of the CallNonvirtualMethodA method calling routines according to their result types. Note that CallNonvirtualMethodA is not a real function name, nor is NativeType a real type name. You should replace CallNonvirtualMethodA with one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-7 CallNonvirtualMethodA Family of Method Calling Routines

CallNonvirtualMethodA Routine Name Native Type Java Type
CallNonvirtualVoidMethodA() void void
CallNonvirtualObjectMethodA() jobject Object
CallNonvirtualBooleanMethodA() jboolean boolean
CallNonvirtualByteMethodA() jbyte byte
CallNonvirtualCharMethodA() jchar char
CallNonvirtualShortMethodA() jshort short
CallNonvirtualIntMethodA() jint int
CallNonvirtualLongMethodA() jlong long
CallNonvirtualFloatMethodA() jfloat float
CallNonvirtualDoubleMethodA() jdouble double

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class.

obj: a Java object.

methodID: a method ID.

args: an array of arguments.

RETURNS:

Returns the result of calling the Java method.

CallNonvirtualMethodV

NativeType CallNonvirtualMethodV(JNIEnv *env, jobject obj,
jclass clazz, jmethodID methodID, va_list args);

This family of operations invokes an instance (non-static) method on a Java object, according to the specified class and method ID. The methodID argument must be obtained by calling GetMethodID() on the class clazz.

The CallNonvirtualMethodV family of routines and the CallMethodV family of routines are different. The CallMethodV routines invoke the method based on the class of the object, while the CallNonvirtualMethodV routines invoke the method based on the class, designated by the clazz parameter, from which the method ID is obtained. The method ID must be obtained from the real class of the object or from one of its superclasses.

Programmers place all arguments to the method in an args argument of type va_list that immediately follows the methodID argument. The CallNonvirtualMethodV routine accepts the arguments, and, in turn, passes them to the Java method that the programmer wishes to invoke.

Table 4-8 describes each of the method calling routines according to their result types. Note that CallNonvirtualMethodV is not a real function name, nor is NativeType a real type name. You should replace CallNonvirtualMethodV with one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-8 CallNonvirtualMethodV Family of Method Calling Routines

CallNonvirtualMethodV Routine Name Native Type Java Type
CallNonvirtualVoidMethodV() void void
CallNonvirtualObjectMethodV() jobject Object
CallNonvirtualBooleanMethodV() jboolean boolean
CallNonvirtualByteMethodV() jbyte byte
CallNonvirtualCharMethodV() jchar char
CallNonvirtualShortMethodV() jshort short
CallNonvirtualIntMethodV() jint int
CallNonvirtualLongMethodV() jlong long
CallNonvirtualFloatMethodV() jfloat float
CallNonvirtualDoubleMethodV() jdouble double

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class.

obj: a Java object.

methodID: a method ID.

args: a va_list of arguments.

RETURNS:

Returns the result of calling the Java method.

Accessing Static Fields

GetStaticFieldID

jfieldID GetStaticFieldID(JNIEnv *env, jclass clazz,
const char *name, const char *sig);

Returns the field ID for a static field of a class. The field is specified by its name and signature. The GetStaticField and SetStaticField families of accessor functions use field IDs to retrieve static fields.

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

name: the static field name in a 0-terminated UTF-8 string.

sig: the field signature in a 0-terminated UTF-8 string.

RETURNS:

Returns a field ID, or NULL if the specified static field cannot be found.

THROWS:

NoSuchFieldError: if the specified static field cannot be found.

GetStaticField

NativeType GetStaticField(JNIEnv *env, jclass clazz,
jfieldID fieldID);

This family of accessor routines returns the value of a static field of an object. The field to access is specified by a field ID, which is obtained by calling GetStaticFieldID().

Table 4-9 describes the family of get routine names and result types. Note that GetStaticField is not a real function name, nor is NativeType a real type name. You should replace GetStaticField with one of the actual static field accessor routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-9 GetStaticField Family of Accessor Routines

GetStaticField Routine Name Native Type Java Type
GetStaticObjectField() jobject Object
GetStaticBooleanField() jboolean boolean
GetStaticByteField() jbyte byte
GetStaticCharField() jchar char
GetStaticShortField() jshort short
GetStaticIntField() jint int
GetStaticLongField() jlong long
GetStaticFloatField() jfloat float
GetStaticDoubleField() jdouble double

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

fieldID: a static field ID.

RETURNS:

Returns the content of the static field.

SetStaticField

void SetStaticField(JNIEnv *env, jclass clazz,
jfieldID fieldID,
NativeType value);

This family of accessor routines sets the value of a static field of an object. The field to access is specified by a field ID, which is obtained by calling GetStaticFieldID().

Table 4-10 describes the set routine name and value types. Note that SetStaticField is not a real function name, nor is NativeType a real type name. You should replace SetStaticField with one of the actual set static field routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-10 SetStaticField Family of Accessor Routines

SetStaticField Routine Name NativeType Java Type
SetStaticObjectField() jobject Object
SetStaticBooleanField() jboolean boolean
SetStaticByteField() jbyte byte
SetStaticCharField() jchar char
SetStaticShortField() jshort short
SetStaticIntField() jint int
SetStaticLongField() jlong long
SetStaticFloatField() jfloat float
SetStaticDoubleField() jdouble double

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

fieldID: a static field ID.

value: the new value of the field.

Calling Static Methods

GetStaticMethodID

jmethodID GetStaticMethodID(JNIEnv *env, jclass clazz,
const char *name, const char *sig);

Returns the method ID for a static method of a class. The method is specified by its name and signature.

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

name: the static method name in a 0-terminated UTF-8 string.

sig: the method signature in a 0-terminated UTF-8 string.

RETURNS:

Returns a method ID, or NULL if the operation fails.

THROWS:

NoSuchMethodError: if the specified static method cannot be found.

CallStaticMethod

NativeType CallStaticMethod(JNIEnv *env, jclass clazz,
jmethodID methodID, ...);

This family of operations invokes a static method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetStaticMethodID().

The method ID must be derived from clazz, not from one of its superclasses.

Programmers place all arguments that are to be passed to the method immediately following the methodID argument. The CallStaticMethod routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke.

Table 4-11 describes each of the method calling routines according to their result types. Note that CallStaticMethod is not a real function name, nor is NativeType a real type name. You should replace CallStaticMethod with one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-11 CallStaticMethod Family of Method Calling Routines

CallStaticMethod Routine Native Type Java Type
CallStaticVoidMethod() void void
CallStaticObjectMethod() jobject Object
CallStaticBooleanMethod() jboolean boolean
CallStaticByteMethod() jbyte byte
CallStaticCharMethod() jchar char
CallStaticShortMethod() jshort short
CallStaticIntMethod() jint int
CallStaticLongMethod() jlong long
CallStaticFloatMethod() jfloat float
CallStaticDoubleMethod() jdouble double

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

methodID: a static method ID.

RETURNS:

Returns the result of calling the static Java method.

CallStaticMethodA

NativeType CallStaticMethodA(JNIEnv *env, jclass clazz,
jmethodID methodID, jvalue *args);

This family of operations invokes a static method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetStaticMethodID().

The method ID must be derived from clazz, not from one of its superclasses.

Programmers place all arguments to the method in an args array of jvalues that immediately follows the methodID argument. The CallStaticMethodA routine accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.

Table 4-12 describes each of the static method calling routines according to their result types. Note that CallStaticMethodA is not a real function name, nor is NativeType a real type name. You should replace CallStaticMethodA with one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-12 CallStaticMethodA Family of Method Calling Routines

CallStaticMethodA Routine Name NativeType Java Type
CallStaticVoidMethodA() void void
CallStaticObjectMethodA() jobject Object
CallStaticBooleanMethodA() jboolean boolean
CallStaticByteMethodA() jbyte byte
CallStaticCharMethodA() jchar char
CallStaticShortMethodA() jshort short
CallStaticIntMethodA() jint int
CallStaticLongMethodA() jlong long
CallStaticFloatMethodA() jfloat float
CallStaticDoubleMethodA() jdouble double

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

methodID: a static method ID.

args: an array of arguments.

RETURNS:

Returns the result of calling the static Java method.

CallStaticMethodV

NativeType CallStaticMethodV(JNIEnv *env, jclass clazz,
jmethodID methodID, va_list args);

This family of operations invokes a static method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetStaticMethodID().

The method ID must be derived from clazz, not from one of its superclasses.

Programmers place all arguments to the method in an args argument of type va_list that immediately follows the methodID argument. The CallStaticMethodV routine accepts the arguments, and, in turn, passes them to the Java method that the programmer wishes to invoke.

Table 4-13 describes each of the method calling routines according to their result types. Note that CallStaticMethodV is not a real function name, nor is NativeType a real type name. You should replace CallStaticMethodV with one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-13 CallStaticMethodV Family of Method Calling Routines

CallStaticMethodV Routine Name Native Type Java Type
CallStaticVoidMethodV() void void
CallStaticObjectMethodV() jobject Object
CallStaticBooleanMethodV() jboolean boolean
CallStaticByteMethodV() jbyte byte
CallStaticCharMethodV() jchar char
CallStaticShortMethodV() jshort short
CallStaticIntMethodV() jint int
CallStaticLongMethodV() jlong long
CallStaticFloatMethodV() jfloat float
CallStaticDoubleMethodV() jdouble double

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

methodID: a static method ID.

args: a va_list of arguments.

RETURNS:

Returns the result of calling the static Java method.

String Operations

NewString

jstring NewString(JNIEnv *env, const jchar *unicodeChars,
jsize len);

Constructs a new java.lang.String object from an array of Unicode characters.

PARAMETERS:

env: the JNI interface pointer.

unicodeChars: pointer to a Unicode string.

len: length of the Unicode string.

RETURNS:

Returns a Java string object, or NULL if the string cannot be constructed.

GetStringLength

jsize GetStringLength(JNIEnv *env, jstring string);

Returns the length (the count of Unicode characters) of a Java string.

PARAMETERS:

env: the JNI interface pointer.

string: a Java string object.

RETURNS:

Returns the length of the Java string.

GetStringChars

const jchar * GetStringChars(JNIEnv *env, jstring string,
jboolean *isCopy);

Returns a pointer to the array of Unicode characters of the string. This pointer is valid until ReleaseStringchars() is called.

If isCopy is not NULL, then *isCopy is set to one if a copy is made, or it is set to zero if no copy is made.

PARAMETERS:

env: the JNI interface pointer.

string: a Java string object.

isCopy: a pointer to a boolean.

RETURNS:

Returns a pointer to a Unicode string, or NULL if the operation fails.

ReleaseStringChars

void ReleaseStringChars(JNIEnv *env, jstring string,
const jchar *chars);

Informs the VM that the native code no longer needs access to chars. The chars argument is a pointer obtained from string using GetStringChars().

PARAMETERS:

env: the JNI interface pointer.

string: a Java string object.

chars: a pointer to a Unicode string.

NewStringUTF

jstring NewStringUTF(JNIEnv *env, const char *bytes,
jsize length);

Constructs a new java.lang.String object from an array of UTF-8 characters.

PARAMETERS:

env: the JNI interface pointer, or NULL if the string cannot be constructed.

bytes: the pointer to a UTF-8 string.

length: the length of the UTF-8 string.

RETURNS:

Returns a Java string object, or NULL if the string cannot be constructed.

GetStringUTFLength

jsize GetStringUTFLength(JNIEnv *env, jstring string);

Returns the UTF-8 length in bytes of a string.

PARAMETERS:

env: the JNI interface pointer.

string: a Java string object.

RETURNS:

Returns the UTF-8 length of the string.

GetStringUTFChars

jbyte* GetStringUTFChars(JNIEnv *env, jstring string,
jboolean *isCopy);

Returns a pointer to an array of UTF-8 characters of the string. This array is valid until it is released by ReleaseStringUTFChars().

If isCopy is not NULL, then *isCopy is set to one if a copy is made, or it is set to zero if no copy is made.

PARAMETERS:

env: the JNI interface pointer.

string: a Java string object.

isCopy: a pointer to a boolean.

RETURNS:

Returns a pointer to a UTF-8 string, or NULL if the operation fails.

ReleaseStringUTFChars

void ReleaseStringUTFChars(JNIEnv *env, jstring string,
const jbyte *utf);

Informs the VM that the native code no longer needs access to utf. The utf argument is a pointer derived from string using GetStringUTFChars().

PARAMETERS:

env: the JNI interface pointer.

string: a Java string object.

utf: a pointer to a UTF-8 string.

Array Operations

GetArrayLength

jsize GetArrayLength(JNIEnv *env, jarray array);

Returns the number of elements in the array.

PARAMETERS:

env: the JNI interface pointer.

array: a Java array object.

RETURNS:

Returns the length of the array.

NewObjectArray

jarray NewObjectArray(JNIEnv *env, jsize length,
jclass elementClass, jobject initialElement);

Constructs a new array holding objects in class elementClass. All elements are initially set to initialElement.

PARAMETERS:

env: the JNI interface pointer.

length: array size.

elementClass: array element class.

initialElement: initialization value.

RETURNS:

Returns a Java array object, or NULL if the array cannot be constructed.

GetObjectArrayElement

jobject GetObjectArrayElement(JNIEnv *env, jarray array,
jsize index);

Returns an element of an Object array.

PARAMETERS:

env: the JNI interface pointer.

array: a Java array.

index: array index.

RETURNS:

Returns a Java object.

THROWS:

ArrayIndexOutOfBoundsException: if index does not specify a valid index in the array.

SetObjectArrayElement

void SetObjectArrayElement(JNIEnv *env, jarray array,
jsize index, jobject value);

Sets an element of an Object array.

PARAMETERS:

env: the JNI interface pointer.

array: a Java array.

index: array index.

value: the new value.

THROWS:

ArrayIndexOutOfBoundsException: if index does not specify a valid index in the array.

ArrayStoreException: if the class of value is not a subclass of the element class of the array.

NewScalarArray

jarray NewScalarArray(JNIEnv *env, jsize length);

A family of operations to construct a new scalar array object. Table 4-14 describes the specific scalar array constructors. Note that NewScalarArray is not a real function name. You should replace NewScalarArray with one of the actual scalar array constructor routine names from the table.

Table 4-14 NewScalarArray Family of Scalar Array Constructors

NewScalarArray Constructors Java Array Type
NewBooleanArray() boolean[]
NewByteArray() byte[]
NewCharArray() char[]
NewShortArray() short[]
NewIntArray() int[]
NewLongArray() long[]
NewFloatArray() float[]
NewDoubleArray() double[]

PARAMETERS:

env: the JNI interface pointer.

length: the array length.

RETURNS:

Returns a Java array, or NULL if the array cannot be constructed.

GetScalarArrayElements

NativeArrayType GetScalarArrayElements(JNIEnv *env,
jarray array, jboolean *isCopy);

A family of functions that returns the body of the scalar array. The result is valid until the corresponding ReleaseScalarArrayElements() function is called. Since the returned array may be a copy of the Java array, changes made to the returned array will not necessarily be reflected in the original array until ReleaseScalarArrayElements() is called.

If isCopy is not NULL, then *isCopy is set to one if a copy is made, or it is set to zero if no copy is made.

Table 4-15 describes the specific scalar array element accessors. Note that GetScalarArrayElements is not a real function name, nor is NativeArrayType a real type name. You should replace GetScalarArrayElements with one of the actual scalar element accessor routine names from the table, and replace NativeArrayType with the corresponding native array type for that routine.

Regardless of how boolean arrays are represented in the Java VM, GetBooleanArrayElements() always returns a pointer to jbooleans, with each byte denoting an element (that is, the unpacked representation). All arrays of other types are guaranteed to be contiguous in memory.

Table 4-15 GetScalarArrayElements Family of Accessor Routines

GetScalarArrayElements Routines Native Array Type Java Array Type
GetBooleanArrayElements() jboolean * boolean[]
GetByteArrayElements() jbyte * byte[]
GetCharArrayElements() jchar * char[]
GetShortArrayElements() jshort * short[]
GetIntArrayElements() jint * int[]
GetLongArrayElements() jlong * long[]
GetFloatArrayElements() jfloat * float[]
GetDoubleArrayElements() jdouble * double[]

PARAMETERS:

env: the JNI interface pointer.

array: a Java string object.

isCopy: a pointer to a boolean.

RETURNS:

Returns a pointer to the array elements, or NULL if the operation fails.

ReleaseScalarArrayElements

void ReleaseScalarArrayElements(JNIEnv *env, jarray array,
NativeArrayType elems, jint mode);

A family of functions that informs the VM that the native code no longer needs access to elems. The elems argument is a pointer derived from array using the corresponding GetScalarArrayElements(). If necessary, copies back all changes made to elems to the original array.

The mode argument provides information on how the array buffer should be released. mode has no effect if elems is not a copy of the elements in array. Otherwise, mode has the following impact, as shown in Table 4-16:

Table 4-16 Effect of mode Argument

mode actions
0 copy back the content and free the elems buffer
JNI_COMMIT copy back the content but do not free the elems buffer
JNI_ABORT free the buffer without copying back the possible changes

In most cases, programmers pass zero to the mode argument to ensure consistent behavior for both pinned and copied arrays. The other options give the programmer more control over memory management and must be used with extreme care.

Table 4-17 describes the specific routines that comprise the family of scalar array disposers. Note that ReleaseScalarArrayElements is not a real function name, nor is NativeArrayType a real type name. You should replace ReleaseScalarArrayElements with one of the actual scalar array disposer routine names from the table, and replace NativeArrayType with the corresponding native array type for that routine.

Table 4-17 ReleaseScalarArrayElements Family of Scalar Array Disposer Routines

ReleaseScalarArrayElements Routines Native Array Type Java Array Type
ReleaseBooleanArrayElements() jboolean * boolean[]
ReleaseByteArrayElements() jbyte * byte[]
ReleaseCharArrayElements() jchar * char[]
ReleaseShortArrayElements() jshort * short[]
ReleaseIntArrayElements() jint * int[]
ReleaseLongArrayElements() jlong * long[]
ReleaseFloatArrayElements() jfloat * float[]
ReleaseDoubleArrayElements() jdouble * double[]

PARAMETERS:

env: the JNI interface pointer.

array: a Java array object.

elems: a pointer to array elements.

mode: the release mode.

GetScalarArrayRegion

void GetScalarArrayRegion(JNIEnv *env, jarray array,
jsize start, jsize len,
NativeType *buf);

A family of functions that copies a region of a scalar array into a buffer.

Table 4-18 describes the specific scalar array element accessors. Note that GetScalarArrayRegion is not a real function name, nor is NativeType a real type name. You should replace GetScalarArrayRegion with one of the actual scalar element accessor routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-18 GetScalarArrayRegion Family of Scalar Element Accessor Routines

GetScalarArrayRegion Routine Native Type Java Array Type
GetBooleanArrayRegion() jboolean boolean[]
GetByteArrayRegion() jbyte byte[]
GetCharArrayRegion() jchar char[]
GetShortArrayRegion() jshort short[]
GetIntArrayRegion() jint int[]
GetLongArrayRegion() jlong long[]
GetFloatArrayRegion() jfloat float[]
GetDoubleArrayRegion() jdouble double[]

PARAMETERS:

env: the JNI interface pointer.

array: a Java array.

start: the starting index.

len: the number of elements to be copied.

buf: the destination buffer.

THROWS:

ArrayIndexOutOfBoundsException: if (start + len - 1) does not specify a valid index in the array.

SetScalarArrayRegion

void SetScalarArrayRegion(JNIEnv *env, jarray array,
jsize start, jsize len,
NativeType *buf);

A family of functions that copies back a region of a scalar array from a buffer.

Table 4-19 describes the specific scalar array element accessors. Note that SetScalarArrayRegion is not a real function name, nor is NativeType a real type name. You should replace SetScalarArrayRegion with one of the actual scalar element accessor routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-19 SetScalarArrayRegion Family of Scalar Element Accessor Routines

SetScalarArrayRegion Routine Native Type Java Array Type
SetBooleanArrayRegion() jboolean boolean[]
SetByteArrayRegion() jbyte byte[]
SetCharArrayRegion() jchar char[]
SetShortArrayRegion() jshort short[]
SetIntArrayRegion() jint int[]
SetLongArrayRegion() jlong long[]
SetFloatArrayRegion() jfloat float[]
SetDoubleArrayRegion() jdouble double[]

PARAMETERS:

env: the JNI interface pointer.

array: a Java array.

start: the starting index.

len: the number of elements to be copied.

buf: the destination buffer.

THROWS:

ArrayIndexOutOfBoundsException: if (start + len - 1)does not specify a valid index in the array.

Registering Native Methods

RegisterNatives

jint RegisterNatives(JNIEnv *env, jclass clazz,
char** nameArray, char** sigArray,
void** nativeProcArray);

Registers native methods with the class specified by the clazz argument. The nameArray parameter specifies a NULL-terminated array of strings containing the names of the native methods. The sigArray parameter specifies a NULL-terminated array of strings containing the signatures of the native methods. The nativeProcArray parameter specifies the array of corresponding native method procedure addresses. All three arrays should be the same length.

The native methods nominally must have the following signature:
ReturnType (*NativeProc)(JNIEnv *env, jref objectOrClass, ...);

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

nameArray: the names of native methods in the class.

sigArray: the signatures of native methods in the class.

nativeProcArray: an array of pointers to native methods.

RETURNS:

Returns zero on success; otherwise, returns a negative value on failure.

THROWS:

NoSuchMethodError: if a specified method cannot be found or if the method is not native.

UnregisterNatives

jint UnregisterNatives(JNIEnv *env, jclass clazz);

Unregisters native methods of a class. The class goes back to the state before it was linked or registered with its native method functions.

This function should not be used in normal native code. Instead, it provides special programs a way to reload and relink native libraries.

PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

RETURNS:

Returns zero on success; otherwise, returns a negative value on failure.

Monitor Operations

MonitorEnter

jint MonitorEnter(JNIEnv *env, jref ref);

Enters the monitor associated with the underlying Java object referred to by ref.

Each Java objects has a monitor associated with it. If the current thread already owns the monitor associated with ref, it increments a counter in the monitor indicating the number of times this thread has entered the monitor. If the monitor associated with ref is not owned by any thread, the current thread becomes the owner of the monitor, setting the entry count of this monitor to 1. If another thread already owns the monitor associated with ref, the current thread waits until the monitor is released, then tries again to gain ownership.

PARAMETERS:

env: the JNI interface pointer.

ref: a normal Java object or class object.

RETURNS:

Returns zero on success; otherwise, returns a negative value on failure.

MonitorExit

jint MonitorExit(JNIEnv *env, jref ref);

The current thread must be the owner of the monitor associated with the underlying Java object referred to by ref. The thread decrements the counter indicating the number of times it has entered this monitor. If as a result the value of the counter becomes zero, the current thread releases the montior.

PARAMETERS:

env: the JNI interface pointer.

ref: a normal Java object or class object.

RETURNS:

Returns zero on success; otherwise, returns a negative value on failure.

Java VM Interface

GetJavaVM

jint GetJavaVM(JNIEnv *env, JavaVM **vm);

Returns the Java VM interface (used in the Invocation API) associated with the current thread. The result is placed at the location pointed to by the second argument, vm.

PARAMETERS:

env: the JNI interface pointer.

vm: a pointer to where the result should be placed.

RETURNS:

Returns zero on success; otherwise, returns a negative value on failure.


Contents | Prev | Next

Java Native Method Interface Specification (HTML generated by wegis on December 06, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to sl@eng.sun.com