Contents | Previous | Next | Java Native Interface Specification |
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 JNI function table.
Note the use of the term “must” to describe restrictions on JNI programmers. For example, when you see that a certain JNI function must receive a non-NULL object, it is your responsibility to ensure that NULL is not passed to that JNI function. As a result, a JNI implementation does not need to perform NULL pointer checks in that JNI function.
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:
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:
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.
const struct JNINativeInterface ... = {NULL
,NULL
,NULL
,NULL
, GetVersion, DefineClass, FindClass,NULL
,NULL
,NULL
, GetSuperclass, IsAssignableFrom,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, };
jint GetVersion(JNIEnv *env);
Returns the version of the native method interface.
env
: the JNI interface pointer.
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.
jclass DefineClass(JNIEnv *env, jobject loader,
const jbyte *buf, jsize bufLen);
Loads a class from a buffer of raw class data. The buffer containing the raw class data is not referenced by the VM after the DefineClass call returns, and it may be discarded if desired.
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 a Java class object or NULL
if an error occurs.
ClassFormatError
: if the class data does not specify a valid class.
ClassCircularityError
: if a class or interface would be its own superclass or superinterface.
OutOfMemoryError
: if the system runs out of memory.
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.
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 a class object from a fully-qualified name, or NULL
if the class cannot be found.
ClassFormatError
: if the class data does not specify a valid class.
ClassCircularityError
: if a class or interface would be its own superclass or superinterface.
NoClassDefFoundError
: if no definition for a requested class or interface can be found.
OutOfMemoryError
: if the system runs out of memory.
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
.
env
: the JNI interface pointer.
clazz
: a Java class object.
Returns the superclass of the class represented by clazz
, or NULL
.
jboolean IsAssignableFrom(JNIEnv *env, jclass clazz1,
jclass clazz2);
Determines whether an object of clazz1
can be safely cast to clazz2
.
env
: the JNI interface pointer.
clazz1
: the first class argument.
clazz2
: the second class argument.
Returns JNI_TRUE
if either of the following is true:
jint Throw(JNIEnv *env, jthrowable obj);
Causes a java.lang.Throwable
object to be thrown.
env
: the JNI interface pointer.
obj
: a java.lang.Throwable
object.
Returns 0 on success; a negative value on failure.
the
java.lang.Throwable
object
obj
.
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.
env
: the JNI interface pointer.
clazz
: a subclass of java.lang.Throwable
.
message
: the message used to construct the java.lang.Throwable
object.
Returns 0 on success; a negative value on failure.
the newly constructed
java.lang.Throwable
object.
jthrowable 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.
env
: the JNI interface pointer.
Returns the exception object that is currently in the process of being thrown, or NULL
if no exception is currently being thrown.
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.
env
: the JNI interface pointer.
void ExceptionClear(JNIEnv *env);
Clears any exception that is currently being thrown. If no exception is currently being thrown, this routine has no effect.
env
: the JNI interface pointer.
void FatalError(JNIEnv *env, const char *msg);
Raises a fatal error and does not expect the VM to recover. This function does not return.
env
: the JNI interface pointer.
msg
: an error message.
jobject NewGlobalRef(JNIEnv *env, jobject obj);
Creates a new global reference to the object referred to by the obj
argument. The obj
argument may be a global or local reference. Global references must be explicitly disposed of by calling DeleteGlobalRef()
.
env
: the JNI interface pointer.
obj
: a global or local reference.
Returns a global reference, or NULL
if the system runs out of memory.
void DeleteGlobalRef(JNIEnv *env, jobject globalRef);
Deletes the global reference pointed to by globalRef
.
env
: the JNI interface pointer.
globalRef
: a global reference.
void DeleteLocalRef(JNIEnv *env, jobject localRef);
Deletes the local reference pointed to by localRef
.
env
: the JNI interface pointer.
localRef
: a local reference.
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.
env
: the JNI interface pointer.
clazz
: a Java class object.
Returns a Java object, or NULL
if the object cannot be constructed.
InstantiationException
: if the class is an interface or an abstract class.
OutOfMemoryError
: if the system runs out of memory.
jobject NewObject(JNIEnv *env, jclass clazz,
jmethodID methodID, ...);
jobject NewObjectA(JNIEnv *env, jclass clazz,
jmethodID methodID, jvalue *args);
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.
The clazz
argument must not refer to an array class.
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.
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.
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.
env
: the JNI interface pointer.
clazz
: a Java class object.
methodID
: the method ID of the constructor.
arguments to the constructor.
args
: an array of arguments to the constructor.
args
: a va_list of arguments to the constructor.
Returns a Java object, or NULL
if the object cannot be constructed.
InstantiationException
: if the class is an interface or an abstract class.
OutOfMemoryError
: if the system runs out of memory.
Any exceptions thrown by the constructor.
jclass GetObjectClass(JNIEnv *env, jobject obj);
Returns the class of an object.
env
: the JNI interface pointer.
obj
: a Java object (must not be NULL
).
Returns a Java class object.
jboolean IsInstanceOf(JNIEnv *env, jobject obj,
jclass clazz);
Tests whether an object is an instance of a class.
env
: the JNI interface pointer.
obj
: a Java object.
clazz
: a Java class object.
Returns JNI_TRUE
if obj
can be cast to clazz
; otherwise, returns JNI_FALSE
. A NULL
object can be cast to any class.
jboolean IsSameObject(JNIEnv *env, jobject ref1,
jobject ref2);
Tests whether two references refer to the same Java object.
env
: the JNI interface pointer.
ref1
: a Java object.
ref2
: a Java object.
Returns JNI_TRUE
if ref1
and ref2
refer to the same Java object, or are both NULL
; otherwise, returns JNI_FALSE
.
jfieldID GetFieldID(JNIEnv *env, jclass clazz,
const char *name, const char *sig);
Returns the field ID for an instance (nonstatic) field of a class. The field is specified by its name and signature. The Get<type>Field and Set<type>Field families of accessor functions use field IDs to retrieve object fields.
GetFieldID()
causes an uninitialized class to be initialized.
GetFieldID()
cannot be used to obtain the length field of an array. Use GetArrayLength()
instead.
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 a field ID, or NULL
if the operation fails.
NoSuchFieldError
: if the specified field cannot be found.
ExceptionInInitializerError
: if the class initializer fails due to an exception.
OutOfMemoryError
: if the system runs out of memory.
NativeType
Get<type>Field(JNIEnv *env, jobject obj,
jfieldID fieldID);
This family of accessor routines returns the value of an instance (nonstatic) field of an object. The field to access is specified by a field ID obtained by calling GetFieldID()
.
The following table describes the Get<type>Field routine name and result type. You should replace type in Get<type>Field with the Java type of the field, or use one of the actual routine names from the table, and replace NativeType with the corresponding native type for that routine.
env
: the JNI interface pointer.
obj
: a Java object (must not be NULL
).
fieldID
: a valid field ID.
Returns the content of the field.
void
Set<type>Field(JNIEnv *env, jobject obj, jfieldID fieldID,
NativeType
value);
This family of accessor routines sets the value of an instance (nonstatic) field of an object. The field to access is specified by a field ID obtained by calling GetFieldID()
.
The following table describes the Set<type>Field routine name and value type. You should replace type in Set<type>Field with the Java type of the field, or use one of the actual routine names from the table, and replace NativeType with the corresponding native type for that routine.
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.
jmethodID GetMethodID(JNIEnv *env, jclass clazz,
const char *name, const char *sig);
Returns the method ID for an instance (nonstatic) method of a class or interface. The method may be defined in one of the clazz
’s superclasses and inherited by clazz
. The method is determined by its name and signature.
GetMethodID()
causes an uninitialized class to be initialized.
To obtain the method ID of a constructor, supply <init>
as the method name and void
(V
) as the return type.
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 a method ID, or NULL
if the specified method cannot be found.
NoSuchMethodError
: if the specified method cannot be found.
ExceptionInInitializerError
: if the class initializer fails due to an exception.
OutOfMemoryError
: if the system runs out of memory.
NativeType
Call<type>Method(JNIEnv *env, jobject obj,
jmethodID methodID, ...);
NativeType
Call<type>MethodA(JNIEnv *env, jobject obj,
jmethodID methodID, jvalue *args);
NativeType
Call<type>MethodV(JNIEnv *env, jobject obj,
jmethodID methodID, va_list args);
Methods from these three families of operations are used to call a Java instance method from a native method.They only differ in their mechanism for passing parameters to the methods that they call.
These families of operations invoke an instance (nonstatic) method on a Java object, according to the specified method ID. The methodID
argument must be obtained by calling GetMethodID
()
.
When these functions are used to call private methods and constructors, the method ID must be derived from the real class of obj
, not from one of its superclasses.
Programmers place all arguments that are to be passed to the method immediately following the methodID
argument. The Call<type>Method routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke.
Programmers place all arguments to the method in an args
array of jvalues
that immediately follows the methodID
argument. The Call<type>MethodA routine accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.
Programmers place all arguments to the method in an args
argument of type va_list
that immediately follows the methodID
argument. The Call<type>MethodV routine accepts the arguments, and, in turn, passes them to the Java method that the programmer wishes to invoke.
The following table describes each of the method calling routines according to their result type. You should replace type in Call<type>Method with the Java type of the method you are calling (or use one of the actual method calling routine names from the table) and replace NativeType with the corresponding native type for that routine.
env
: the JNI interface pointer.
obj
: a Java object.
methodID
: a method ID.
arguments to the Java method.
args
: an array of arguments.
args
: a va_list of arguments.
Returns the result of calling the Java method.
Exceptions raised during the execution of the Java method.
NativeType
CallNonvirtual<type>Method(JNIEnv *env, jobject obj,
jclass clazz, jmethodID methodID, ...);
NativeType
CallNonvirtual<type>MethodA(JNIEnv *env, jobject obj,
jclass clazz, jmethodID methodID, jvalue *args);
NativeType
CallNonvirtual<type>MethodV(JNIEnv *env, jobject obj,
jclass clazz, jmethodID methodID, va_list args);
These families of operations invoke an instance (nonstatic) 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 CallNonvirtual<type>Method families of routines and the Call<type>Method families of routines are different. Call<type>Method routines invoke the method based on the class of the object, while CallNonvirtual<type>Method 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 CallNonvirtual<type>Method routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke.
Programmers place all arguments to the method in an args
array of jvalues
that immediately follows the methodID
argument. The CallNonvirtual<type>MethodA routine accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.
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.
The following table describes each of the method calling routines according to their result type. You should replace type in CallNonvirtual<type>Method with the Java type of the method, or use one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.
env
: the JNI interface pointer.
clazz:
a
Java class.
obj
: a Java object.
methodID
: a method ID.
arguments to the Java method.
args
: an array of arguments.
args
: a va_list
of arguments.
Exceptions raised during the execution of the Java method.
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 GetStatic<type>Field and SetStatic<type>Field families of accessor functions use field IDs to retrieve static fields.
GetStaticFieldID()
causes an uninitialized class to be initialized.
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 a field ID, or NULL
if the specified static field cannot be found.
NoSuchFieldError
: if the specified static field cannot be found.
ExceptionInInitializerError
: if the class initializer fails due to an exception.
OutOfMemoryError
: if the system runs out of memory.
NativeType
GetStatic<type>Field(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()
.
The following table describes the family of get routine names and result types. You should replace type in GetStatic<type>Field with the Java type of the field, or one of the actual static field accessor routine names from the table, and replace NativeType with the corresponding native type for that routine.
env
: the JNI interface pointer.
clazz
: a Java class object.
fieldID
: a static field ID.
Returns the content of the static field.
void
SetStatic<type>Field(JNIEnv *env, jclass clazz,
NativeType
jfieldID fieldID, 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()
.
The following table describes the set routine name and value types. You should replace type in SetStatic<type>Field with the Java type of the field, or one of the actual set static field routine names from the table, and replace NativeType with the corresponding native type for that routine.
env
: the JNI interface pointer.
clazz
: a Java class object.
fieldID
: a static field ID.
value
: the new value of the field.
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.
GetStaticMethodID()
causes an uninitialized class to be initialized.
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 a method ID, or NULL
if the operation fails.
NoSuchMethodError
: if the specified static method cannot be found.
ExceptionInInitializerError
: if the class initializer fails due to an exception.
OutOfMemoryError
: if the system runs out of memory.
NativeType
CallStatic<type>Method(JNIEnv *env, jclass clazz,
jmethodID methodID, ...);
NativeType
CallStatic<type>MethodA(JNIEnv *env, jclass clazz,
jmethodID methodID, jvalue *args);
NativeType
CallStatic<type>MethodV(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 should place all arguments that are to be passed to the method immediately following the methodID
argument. The CallStatic<type>Method routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke.
Programmers should 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.
Programmers should 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.
The following table describes each of the method calling routines according to their result types. You should replace type in CallStatic<type>Method with the Java type of the method, or one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.
env
: the JNI interface pointer.
clazz
: a Java class object.
methodID
: a static method ID.
arguments to the static method.
args
: an array of arguments.
args
: a va_list
of arguments.
Returns the result of calling the static Java method.
Exceptions raised during the execution of the Java method.
jstring NewString(JNIEnv *env, const jchar *unicodeChars,
jsize len);
Constructs a new java.lang.String
object from an array of Unicode characters.
env
: the JNI interface pointer.
unicodeChars
: pointer to a Unicode string.
len
: length of the Unicode string.
Returns a Java string object, or NULL
if the string cannot be constructed.
OutOfMemoryError
: if the system runs out of memory.
jsize GetStringLength(JNIEnv *env, jstring string);
Returns the length (the count of Unicode characters) of a Java string.
env
: the JNI interface pointer.
string
: a Java string object.
Returns the length of the Java string.
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 JNI_TRUE
if a copy is made; or it is set to JNI_FALSE
if no copy is made.
env
: the JNI interface pointer.
string
: a Java string object.
isCopy
: a pointer to a boolean.
Returns a pointer to a Unicode string, or NULL
if the operation fails.
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()
.
env
: the JNI interface pointer.
string
: a Java string object.
chars
: a pointer to a Unicode string.
jstring NewStringUTF(JNIEnv *env, const char *bytes);
Constructs a new java.lang.String
object from an array of UTF-8 characters.
env
: the JNI interface pointer.
bytes
: the pointer to a UTF-8 string.
Returns a Java string object, or NULL
if the string cannot be constructed.
OutOfMemoryError
: if the system runs out of memory.
jsize GetStringUTFLength(JNIEnv *env, jstring string);
Returns the UTF-8 length in bytes of a string.
env
: the JNI interface pointer.
string
: a Java string object.
Returns the UTF-8 length of the string.
const 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 JNI_TRUE
if a copy is made; or it is set to JNI_FALSE
if no copy is made.
env
: the JNI interface pointer.
string
: a Java string object.
isCopy
: a pointer to a boolean.
Returns a pointer to a UTF-8 string, or NULL
if the operation fails.
void ReleaseStringUTFChars(JNIEnv *env, jstring string,
const char *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()
.
env
: the JNI interface pointer.
string
: a Java string object.
utf
: a pointer to a UTF-8 string.
jsize GetArrayLength(JNIEnv *env, jarray array);
Returns the number of elements in the array.
env
: the JNI interface pointer.
array
: a Java array object.
Returns the length of the array.
jobjectArray 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
.
env
: the JNI interface pointer.
length
: array size.
elementClass
: array element class.
initialElement
: initialization value.
Returns a Java array object, or NULL
if the array cannot be constructed.
OutOfMemoryError
: if the system runs out of memory.
jobject GetObjectArrayElement(JNIEnv *env,
jobjectArray array, jsize index);
Returns an element of an Object
array.
env
: the JNI interface pointer.
array
: a Java array.
index
: array index.
Returns a Java object.
ArrayIndexOutOfBoundsException
: if index
does not specify a valid index in the array.
void SetObjectArrayElement(JNIEnv *env, jobjectArray array,
jsize index, jobject value);
Sets an element of an Object
array.
env
: the JNI interface pointer.
array
: a Java array.
index
: array index.
value
: the new value.
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.
ArrayType
New<PrimitiveType>Array(JNIEnv *env, jsize length);
A family of operations used to construct a new primitive array object. Table 4-8 describes the specific primitive array constructors. You should replace New<PrimitiveType>Array with one of the actual primitive array constructor routine names from the following table, and replace ArrayType with the corresponding array type for that routine.
env
: the JNI interface pointer.
length
: the array length.
Returns a Java array, or NULL
if the array cannot be constructed.
NativeType *
Get<PrimitiveType>ArrayElements(JNIEnv *env,
ArrayType
array, jboolean *isCopy);
A family of functions that returns the body of the primitive array. The result is valid until the corresponding Release<
PrimitiveType>
ArrayElements() 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 Release<PrimitiveType>ArrayElements()
is called.
If isCopy
is not NULL
, then *isCopy
is set to JNI_TRUE
if a copy is made; or it is set to JNI_FALSE
if no copy is made.
The following table describes the specific primitive array element accessors. You should make the following substitutions:
Regardless of how boolean arrays are represented in the Java VM, GetBooleanArrayElements()
always returns a pointer to jbooleans
, with each byte denoting an element (the unpacked representation). All arrays of other types are guaranteed to be contiguous in memory.
env
: the JNI interface pointer.
array
: a Java string object.
isCopy
: a pointer to a boolean.
Returns a pointer to the array elements, or NULL
if the operation fails.
void
Release<PrimitiveType>ArrayElements(JNIEnv *env,
ArrayType
array,
NativeType *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 Get<
PrimitiveType>
ArrayElements() function. If necessary, this function 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 the following table:
In most cases, programmers pass “0” 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 should be used with extreme care.
The next table describes the specific routines that comprise the family of primitive array disposers. You should make the following substitutions:
env
: the JNI interface pointer.
array
: a Java array object.
elems
: a pointer to array elements.
mode
: the release mode.
void
Get<PrimitiveType>ArrayRegion(JNIEnv *env,
ArrayType array,
NativeType
jsize start, jsize len, *buf);
A family of functions that copies a region of a primitive array into a buffer.
The following table describes the specific primitive array element accessors. You should do the following substitutions:
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.
ArrayIndexOutOfBoundsException
: if one of the indexes in the region is not valid.
void
Set<PrimitiveType>ArrayRegion(JNIEnv *env,
ArrayType array,
NativeType
jsize start, jsize len, *buf);
A family of functions that copies back a region of a primitive array from a buffer.
The following table describes the specific primitive array element accessors. You should make the following replacements:
env
: the JNI interface pointer.
array
: a Java array.
start
: the starting index.
len
: the number of elements to be copied.
buf
: the source buffer.
ArrayIndexOutOfBoundsException
: if one of the indexes in the region is not valid.
jint RegisterNatives(JNIEnv *env, jclass clazz,
const JNINativeMethod *methods, jint nMethods);
Registers native methods with the class specified by the clazz
argument. The methods
parameter specifies an array of JNINativeMethod
structures that contain the names, signatures, and function pointers of the native methods. The nMethods
parameter specifies the number of native methods in the array. The JNINativeMethod
structure is defined as follows:
The function pointers nominally must have the following signature:
env
: the JNI interface pointer.
clazz
: a Java class object.
methods
: the native methods in the class.
nMethods
: the number of native methods in the class.
Returns “0” on success; returns a negative value on failure.
NoSuchMethodError
: if a specified method cannot be found or if the method is not native.
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.
env
: the JNI interface pointer.
clazz
: a Java class object.
Returns “0” on success; returns a negative value on failure.
jint MonitorEnter(JNIEnv *env, jobject obj);
Enters the monitor associated with the underlying Java object referred to by obj
.
Each Java object has a monitor associated with it. If the current thread already owns the monitor associated with obj
, it increments a counter in the monitor indicating the number of times this thread has entered the monitor. If the monitor associated with obj
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 obj
, the current thread waits until the monitor is released, then tries again to gain ownership.
env
: the JNI interface pointer.
obj
: a normal Java object or class object.
Returns “0” on success; returns a negative value on failure.
jint MonitorExit(JNIEnv *env, jobject obj);
The current thread must be the owner of the monitor associated with the underlying Java object referred to by obj
. The thread decrements the counter indicating the number of times it has entered this monitor. If the value of the counter becomes zero, the current thread releases the monitor.
env
: the JNI interface pointer.
obj
: a normal Java object or class object.
Returns “0” on success; returns a negative value on failure.
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
.
env
: the JNI interface pointer.
vm
: a pointer to where the result should be placed.
Returns “0” on success; returns a negative value on failure.
Contents | Previous | Next |
Copyright © 2003 Sun Microsystems, Inc. All rights reserved.