Package-level declarations
Core bindings: all JNI types, the JNI object with version constants, the Invocation API (createJavaVM, attachCurrentThread, …), every function of the JNI JniEnv interface, and Kotlin utilities around them.
Getting a JNI environment
JNI functions require a JniEnv in context (context(env: JniEnv)).
Exported native methods (e.g. an
@JniActual) - the JVM already provides the environment. Declaringcontext(env: JniEnv)is optional; the producer injects it when you ask for it. A native allocation scope (AutofreeScope,MemScope, …) can be requested the same way.Worker threads - keep a
JavaVMfromJNI_OnLoad(or JNI.javaVMs) and call JavaVM.attachCurrentThread / JavaVM.withEnvAttaching / JavaVM.useEnvAttaching.Embedding a JVM into a native process - JNI.createJavaVM with buildJavaVMInitArgs, then
env.use { … }(see JniEnv.use).
Version constants live on the JNI object (JNI.v1 … JNI.v24). Pass the version you need to getEnv / attachCurrentThread / createJavaVM.
Types
JNI types are null-safe and grouped by purpose:
Primitives: JBoolean, JByte, JChar, JShort, JInt, JLong, JFloat, JDouble, JVoid. A JNI
booleanis an unsignedUByte. Pass Kotlin primitives as-is in most APIs (Boolean,Char, …); use the JNI object types (JBoolean,JChar) only where the raw representation matters. Crossing the boundary:String.toJString()/JString.toKString(),ByteArray.toJArray()/JByteArray.toKArray(), and the analogous helpers for the other primitive arrays.Object references: JObject, JClass, JThrowable, JString, JArray and the typed arrays JBooleanArray…JDoubleArray, JObjectArray, JWeak (not counted by GC), JByteBuffer (direct buffers). They behave like typed pointers - compare with
isSame, check type withinstanceOf, readrefType(JObjectRefType), and convert withunsafeCast()only when you know the type.IDs: JFieldID, JMethodID from fieldId / staticFieldId / methodId / staticMethodId.
Arguments: JValue is the union for Java method arguments. Build a JArguments array with jArgs:
val args = jArgs(3) { int(42); ref(jString); ref(null) }Content copied to clipboardVM types: JavaVM, JniEnv, JavaVMInitArgs, JavaVMOption, JniVersion. Build init args with
buildJavaVMInitArgs(JNI.v21, n) { option(...) }.Native method registration: JniNativeMethod and the registerNatives DSL:
clazz.registerNatives(1) { register("impl".utf8, "(Ljava/lang/String;)I".utf8, staticCFunction { … }) }.
Functions
Usage is close to the C++ style: env->FindClass(className.c_str()) becomes findClass(className.utf8). The differences:
Type safety - no
jobjectcasts; null-safety throughout.Errors as exceptions - functions that return a JNI error code throw a Kotlin exception instead (JniThreadDetachedException, JniVersionException, JniOutOfMemoryException, JniVmAlreadyExistsException).
context(JniEnv)- the environment is never passed as an ordinary parameter.Modified UTF-8 - strings that cross into JNI must be null-terminated modified UTF-8. Prefer String.modifiedUtf8, or the Kotlin
Stringoverloads below. StandardString.utf8differs only for'\u0000'and non-BMP characters, so it is safe when the string has neither.
The full JniEnv surface is covered: class operations (findClass, defineClass, superclass, isAssignableFrom, registerNatives/unregisterNatives, and on desktop module since JDK 9), exceptions (throwEx, throwNew, pendingException, handleJvmException, printStackTrace, clearException, isExceptionThrown, fatalError), local references (deleteLocalRef, newLocalRef, pushLocalFrame, popLocalFrame, refFrame/fromRefFrame), monitors (monitorEnter, monitorExit), strings (newString, length, getChars, getUTFChars, getRegion, getUTFRegion, utfLength, and on desktop utfLengthLong since JDK 24, getCharsCritical), arrays (newXArray, getRegion, setRegion, getElements, releaseElements with an ApplyChangesMode, getElementsCritical), object fields and methods (see #package-iogithubmimimishkinjnibindingaccessors for the typed variants), reflection (fromReflectedMethod, toReflectedMethod, …), direct buffers (newDirectByteBuffer, address, capacity) and, on desktop, isVirtualThread since JDK 21.
Kotlin String overloads
Most lookup and message APIs have overloads that take a Kotlin String instead of a CValuesRef<ByteVar>. They encode with String.modifiedUtf8 and throw when the lookup fails, instead of returning null:
context(env: JniEnv, autofreeScope: AutofreeScope)
fun example() {
val clazz = findClass("java/lang/String")
val mid = clazz.methodId("length", "()I")
throwNew(clazz, "something went wrong")
}Available for findClass, defineClass, methodId / staticMethodId, fieldId / staticFieldId, throwNew, fatalError, attachCurrentThread / attachCurrentThreadAsDaemon, withEnvAttaching, and registerNatives { register(name, sig, fn) }.
Utilities
String.modifiedUtf8 - correct modified UTF-8 encoding (Kotlin
'\u0000'→0xC0 0x80, non-BMP as surrogate pairs). Required by Modified UTF-8 Strings in the JNI specification.JniEnv.use / JavaVM.useEnv / JavaVM.useEnvAttaching - scope an environment together with a native
MemScopeso allocations inside (UTF-8 values, jArgs arrays, …) are freed automatically.JavaVM.withEnv / JavaVM.withEnvAttaching - obtain an environment for the current thread (and detach when done, for the attaching variants).
jniLazy / JniLazy - lazily initialized values whose
context(JniEnv, MemScope)initializer runs once, in an attached environment, with synchronized initialization. Useful for cachingJClass/JMethodIDacross calls.localIntoWeakRef / localIntoGlobalRef - elevate a local reference and delete the local one in one step.
Core bindings: all JNI types, the JNI object with version constants, the Invocation API (createJavaVM, attachCurrentThread, …), every function of the JNI JniEnv interface, and Kotlin utilities around them.
Getting a JNI environment
JNI functions require a JniEnv in context (context(env: JniEnv)).
Exported native methods (e.g. an
@JniActual) - the JVM already provides the environment. Declaringcontext(env: JniEnv)is optional; the producer injects it when you ask for it. A native allocation scope (AutofreeScope,MemScope, …) can be requested the same way.Worker threads - keep a
JavaVMfromJNI_OnLoad(or JNI.javaVMs) and call JavaVM.attachCurrentThread / JavaVM.withEnvAttaching / JavaVM.useEnvAttaching.Embedding a JVM into a native process - JNI.createJavaVM with buildJavaVMInitArgs, then
env.use { … }(see JniEnv.use).
Version constants live on the JNI object (JNI.v1 … JNI.v24). Pass the version you need to getEnv / attachCurrentThread / createJavaVM.
Types
JNI types are null-safe and grouped by purpose:
Primitives: JBoolean, JByte, JChar, JShort, JInt, JLong, JFloat, JDouble, JVoid. A JNI
booleanis an unsignedUByte. Pass Kotlin primitives as-is in most APIs (Boolean,Char, …); use the JNI object types (JBoolean,JChar) only where the raw representation matters. Crossing the boundary:String.toJString()/JString.toKString(),ByteArray.toJArray()/JByteArray.toKArray(), and the analogous helpers for the other primitive arrays.Object references: JObject, JClass, JThrowable, JString, JArray and the typed arrays JBooleanArray…JDoubleArray, JObjectArray, JWeak (not counted by GC), JByteBuffer (direct buffers). They behave like typed pointers - compare with
isSame, check type withinstanceOf, readrefType(JObjectRefType), and convert withunsafeCast()only when you know the type.IDs: JFieldID, JMethodID from fieldId / staticFieldId / methodId / staticMethodId.
Arguments: JValue is the union for Java method arguments. Build a JArguments array with jArgs:
val args = jArgs(3) { int(42); ref(jString); ref(null) }Content copied to clipboardVM types: JavaVM, JniEnv, JavaVMInitArgs, JavaVMOption, JniVersion. Build init args with
buildJavaVMInitArgs(JNI.v21, n) { option(...) }.Native method registration: JniNativeMethod and the registerNatives DSL:
clazz.registerNatives(1) { register("impl".utf8, "(Ljava/lang/String;)I".utf8, staticCFunction { … }) }.
Functions
Usage is close to the C++ style: env->FindClass(className.c_str()) becomes findClass(className.utf8). The differences:
Type safety - no
jobjectcasts; null-safety throughout.Errors as exceptions - functions that return a JNI error code throw a Kotlin exception instead (JniThreadDetachedException, JniVersionException, JniOutOfMemoryException, JniVmAlreadyExistsException).
context(JniEnv)- the environment is never passed as an ordinary parameter.Modified UTF-8 - strings that cross into JNI must be null-terminated modified UTF-8. Prefer String.modifiedUtf8, or the Kotlin
Stringoverloads below. StandardString.utf8differs only for'\u0000'and non-BMP characters, so it is safe when the string has neither.
The full JniEnv surface is covered: class operations (findClass, defineClass, superclass, isAssignableFrom, registerNatives/unregisterNatives, and on desktop module since JDK 9), exceptions (throwEx, throwNew, pendingException, handleJvmException, printStackTrace, clearException, isExceptionThrown, fatalError), local references (deleteLocalRef, newLocalRef, pushLocalFrame, popLocalFrame, refFrame/fromRefFrame), monitors (monitorEnter, monitorExit), strings (newString, length, getChars, getUTFChars, getRegion, getUTFRegion, utfLength, and on desktop utfLengthLong since JDK 24, getCharsCritical), arrays (newXArray, getRegion, setRegion, getElements, releaseElements with an ApplyChangesMode, getElementsCritical), object fields and methods (see #package-iogithubmimimishkinjnibindingaccessors for the typed variants), reflection (fromReflectedMethod, toReflectedMethod, …), direct buffers (newDirectByteBuffer, address, capacity) and, on desktop, isVirtualThread since JDK 21.
Kotlin String overloads
Most lookup and message APIs have overloads that take a Kotlin String instead of a CValuesRef<ByteVar>. They encode with String.modifiedUtf8 and throw when the lookup fails, instead of returning null:
context(env: JniEnv, autofreeScope: AutofreeScope)
fun example() {
val clazz = findClass("java/lang/String")
val mid = clazz.methodId("length", "()I")
throwNew(clazz, "something went wrong")
}Available for findClass, defineClass, methodId / staticMethodId, fieldId / staticFieldId, throwNew, fatalError, attachCurrentThread / attachCurrentThreadAsDaemon, withEnvAttaching, and registerNatives { register(name, sig, fn) }.
Utilities
String.modifiedUtf8 - correct modified UTF-8 encoding (Kotlin
'\u0000'→0xC0 0x80, non-BMP as surrogate pairs). Required by Modified UTF-8 Strings in the JNI specification.JniEnv.use / JavaVM.useEnv / JavaVM.useEnvAttaching - scope an environment together with a native
MemScopeso allocations inside (UTF-8 values, jArgs arrays, …) are freed automatically.JavaVM.withEnv / JavaVM.withEnvAttaching - obtain an environment for the current thread (and detach when done, for the attaching variants).
jniLazy / JniLazy - lazily initialized values whose
context(JniEnv, MemScope)initializer runs once, in an attached environment, with synchronized initialization. Useful for cachingJClass/JMethodIDacross calls.localIntoWeakRef / localIntoGlobalRef - elevate a local reference and delete the local one in one step.
Core bindings: all JNI types, the JNI object with version constants, the Invocation API (createJavaVM, attachCurrentThread, …), every function of the JNI JniEnv interface, and Kotlin utilities around them.
Getting a JNI environment
JNI functions require a JniEnv in context (context(env: JniEnv)).
Exported native methods (e.g. an
@JniActual) - the JVM already provides the environment. Declaringcontext(env: JniEnv)is optional; the producer injects it when you ask for it. A native allocation scope (AutofreeScope,MemScope, …) can be requested the same way.Worker threads - keep a
JavaVMfromJNI_OnLoad(or JNI.javaVMs) and call JavaVM.attachCurrentThread / JavaVM.withEnvAttaching / JavaVM.useEnvAttaching.Embedding a JVM into a native process - JNI.createJavaVM with buildJavaVMInitArgs, then
env.use { … }(see JniEnv.use).
Version constants live on the JNI object (JNI.v1 … JNI.v24). Pass the version you need to getEnv / attachCurrentThread / createJavaVM.
Types
JNI types are null-safe and grouped by purpose:
Primitives: JBoolean, JByte, JChar, JShort, JInt, JLong, JFloat, JDouble, JVoid. A JNI
booleanis an unsignedUByte. Pass Kotlin primitives as-is in most APIs (Boolean,Char, …); use the JNI object types (JBoolean,JChar) only where the raw representation matters. Crossing the boundary:String.toJString()/JString.toKString(),ByteArray.toJArray()/JByteArray.toKArray(), and the analogous helpers for the other primitive arrays.Object references: JObject, JClass, JThrowable, JString, JArray and the typed arrays JBooleanArray…JDoubleArray, JObjectArray, JWeak (not counted by GC), JByteBuffer (direct buffers). They behave like typed pointers - compare with
isSame, check type withinstanceOf, readrefType(JObjectRefType), and convert withunsafeCast()only when you know the type.IDs: JFieldID, JMethodID from fieldId / staticFieldId / methodId / staticMethodId.
Arguments: JValue is the union for Java method arguments. Build a JArguments array with jArgs:
val args = jArgs(3) { int(42); ref(jString); ref(null) }Content copied to clipboardVM types: JavaVM, JniEnv, JavaVMInitArgs, JavaVMOption, JniVersion. Build init args with
buildJavaVMInitArgs(JNI.v21, n) { option(...) }.Native method registration: JniNativeMethod and the registerNatives DSL:
clazz.registerNatives(1) { register("impl".utf8, "(Ljava/lang/String;)I".utf8, staticCFunction { … }) }.
Functions
Usage is close to the C++ style: env->FindClass(className.c_str()) becomes findClass(className.utf8). The differences:
Type safety - no
jobjectcasts; null-safety throughout.Errors as exceptions - functions that return a JNI error code throw a Kotlin exception instead (JniThreadDetachedException, JniVersionException, JniOutOfMemoryException, JniVmAlreadyExistsException).
context(JniEnv)- the environment is never passed as an ordinary parameter.Modified UTF-8 - strings that cross into JNI must be null-terminated modified UTF-8. Prefer String.modifiedUtf8, or the Kotlin
Stringoverloads below. StandardString.utf8differs only for'\u0000'and non-BMP characters, so it is safe when the string has neither.
The full JniEnv surface is covered: class operations (findClass, defineClass, superclass, isAssignableFrom, registerNatives/unregisterNatives, and on desktop module since JDK 9), exceptions (throwEx, throwNew, pendingException, handleJvmException, printStackTrace, clearException, isExceptionThrown, fatalError), local references (deleteLocalRef, newLocalRef, pushLocalFrame, popLocalFrame, refFrame/fromRefFrame), monitors (monitorEnter, monitorExit), strings (newString, length, getChars, getUTFChars, getRegion, getUTFRegion, utfLength, and on desktop utfLengthLong since JDK 24, getCharsCritical), arrays (newXArray, getRegion, setRegion, getElements, releaseElements with an ApplyChangesMode, getElementsCritical), object fields and methods (see #package-iogithubmimimishkinjnibindingaccessors for the typed variants), reflection (fromReflectedMethod, toReflectedMethod, …), direct buffers (newDirectByteBuffer, address, capacity) and, on desktop, isVirtualThread since JDK 21.
Kotlin String overloads
Most lookup and message APIs have overloads that take a Kotlin String instead of a CValuesRef<ByteVar>. They encode with String.modifiedUtf8 and throw when the lookup fails, instead of returning null:
context(env: JniEnv, autofreeScope: AutofreeScope)
fun example() {
val clazz = findClass("java/lang/String")
val mid = clazz.methodId("length", "()I")
throwNew(clazz, "something went wrong")
}Available for findClass, defineClass, methodId / staticMethodId, fieldId / staticFieldId, throwNew, fatalError, attachCurrentThread / attachCurrentThreadAsDaemon, withEnvAttaching, and registerNatives { register(name, sig, fn) }.
Utilities
String.modifiedUtf8 - correct modified UTF-8 encoding (Kotlin
'\u0000'→0xC0 0x80, non-BMP as surrogate pairs). Required by Modified UTF-8 Strings in the JNI specification.JniEnv.use / JavaVM.useEnv / JavaVM.useEnvAttaching - scope an environment together with a native
MemScopeso allocations inside (UTF-8 values, jArgs arrays, …) are freed automatically.JavaVM.withEnv / JavaVM.withEnvAttaching - obtain an environment for the current thread (and detach when done, for the attaching variants).
jniLazy / JniLazy - lazily initialized values whose
context(JniEnv, MemScope)initializer runs once, in an attached environment, with synchronized initialization. Useful for cachingJClass/JMethodIDacross calls.localIntoWeakRef / localIntoGlobalRef - elevate a local reference and delete the local one in one step.
Types
The underlying opaque type for JBooleanArray.
The underlying opaque type for JByteArray.
The underlying open opaque type for JByteBuffer.
The underlying opaque type for JCharArray.
The underlying opaque type for JDoubleArray.
The underlying opaque type for JFloatArray.
The underlying opaque type for JIntArray.
The underlying opaque type for JLongArray.
The underlying opaque type for JMethodID.
The underlying open opaque type for JObjectArray.
The underlying sealed opaque type for JPrimitiveArray.
The underlying opaque type for JShortArray.
The underlying open opaque type for JThrowable.
Specifies the mode for applying changes in releaseElements and releaseElementsCritical functions.
C array of JValue.
Special type that exposes methods to add arguments:
Type allowing to operate with Invocation API.
Thread attachment arguments.
Thread attachment arguments.
Thread attachment arguments.
Java VM initialization arguments structure.
Java VM initialization arguments structure.
Java VM initialization arguments structure.
Special type that exposes option method to add VM options. It's used in buildJavaVMInitArgs function.
Java VM initialization option.
Java VM initialization option.
Java VM initialization option.
Pointer to a boolean[] object.
Pointer to a byte[] object.
Pointer to java.nio.ByteBuffer.
Pointer to a char[] object.
Pointer to a double[] object.
Pointer to a float[] object.
Pointer to an int[] object.
Pointer to a long[] object.
Java unique method ID.
Type allowing to operate with Native JNI API.
Represents a native method specification.
Represents a native method specification.
Represents a native method specification.
Special type that exposes method JNINativeMethodRegistry.register to register native methods. Is used in registerNatives function.
JNI_ENOMEM: the Java VM could not allocate the memory the call needs.
JNI_EDETACHED: the current thread is not attached to the Java VM.
Java version that corresponds to the new JNI API.
JNI_EVERSION: the requested JNI version is not supported by the Java VM.
JNI_EEXIST: a Java VM has already been created in this process, and JNI does not support more than one.
Pointer to an array of an object type.
Represents the type of JNI reference.
Pointer to a primitive array.
Pointer to a short[] object.
Pointer to java.lang.Throwable.
Union in which JNI expect arguments to pass to Java functions.
Union in which JNI expect arguments to pass to Java functions.
Union in which JNI expect arguments to pass to Java functions.
Properties
Fetches and returns the starting address of the memory region referenced by the given direct java.nio.Buffer.
Fetches and returns the capacity of the memory region referenced by the given direct java.nio.Buffer. The capacity is the number of elements that the memory region contains.
Whether unrecognized options are ignored.
A convenient way to check for pending exceptions without creating a local reference to the exception object.
The version of the native method interface.
Converts a String into a null-terminated, modified UTF-8 encoded byte sequence for interoperation with JNI functions.
Returns the java.lang.Module object for the module that the class is a member of. If the class is not in a named module, then the unnamed module of the class loader for the class is returned. If the class represents an array type, then this function returns the Module object for the element type. If the class represents a primitive type or void, then the Module object for the java.base module is returned.
The option as a Kotlin string.
The option list.
Exception object currently in the process of being thrown, or null if there is no one.
Returns the type of the object referred to by the obj argument.
If this class represents any class other than the class Object, then it is the object that represents the superclass of the class specified by this class.
The length in bytes of the modified UTF-8 representation of a string.
Functions
Allocates a new Java object without invoking any of the constructors for the object.
Returns an Iterable over the elements of the object array. As the array may contain null elements or elements of a different type, this iterable is strongly typed.
inline fun JavaVM.attachCurrentThread(version: JniVersion, name: String, group: JObject? = null): JniEnv
Alias for JavaVM.attachCurrentThread which accepts the thread name as a Kotlin String.
fun JavaVM.attachCurrentThread(version: JniVersion, name: CValuesRef<ByteVar>? = null, group: JObject? = null): JniEnv
Attaches the current thread to a Java VM. Returns a JniEnv.
inline fun JavaVM.attachCurrentThreadAsDaemon(version: JniVersion, name: String, group: JObject? = null): JniEnv
Alias for JavaVM.attachCurrentThreadAsDaemon which accepts the thread name as a Kotlin String.
fun JavaVM.attachCurrentThreadAsDaemon(version: JniVersion, name: CValuesRef<ByteVar>? = null, group: JObject? = null): JniEnv
Same semantics as attachCurrentThread, but the newly created java.lang.Thread instance is a daemon.
Add boolean value to arguments.
inline fun buildJavaVMInitArgs(version: JniVersion, optionsCount: Int, ignoreUnrecognized: Boolean = false, block: JavaVMInitArgsBuilder.() -> Unit): JavaVMInitArgs
Allocates and initializes JavaVMInitArgs.
Add byte value to arguments.
Unifies usage of call<type>Method methods.
fun JObject.callNonvirtualBooleanMethod(clazz: JClass, methodId: JMethodID, args: JArguments): Boolean
fun JObject.callNonvirtualByteMethod(clazz: JClass, methodId: JMethodID, args: JArguments): Byte
fun JObject.callNonvirtualCharMethod(clazz: JClass, methodId: JMethodID, args: JArguments): Char
fun JObject.callNonvirtualDoubleMethod(clazz: JClass, methodId: JMethodID, args: JArguments): Double
fun JObject.callNonvirtualFloatMethod(clazz: JClass, methodId: JMethodID, args: JArguments): Float
fun JObject.callNonvirtualIntMethod(clazz: JClass, methodId: JMethodID, args: JArguments): Int
fun JObject.callNonvirtualLongMethod(clazz: JClass, methodId: JMethodID, args: JArguments): Long
inline fun <R> JObject.callNonvirtualMethod(clazz: JClass, methodId: JMethodID, args: JArguments): R
Unifies usage of callNonvirtual<type>Method methods, returning method with the appropriate type wrapped in lambda.
fun JObject.callNonvirtualObjectMethod(clazz: JClass, methodId: JMethodID, args: JArguments): JObject?
fun JObject.callNonvirtualShortMethod(clazz: JClass, methodId: JMethodID, args: JArguments): Short
fun JObject.callNonvirtualVoidMethod(clazz: JClass, methodId: JMethodID, args: JArguments)
fun JClass.callStaticBooleanMethod(methodId: JMethodID, args: JArguments): Boolean
Invokes a static method on a Java object, according to the specified method ID. The methodId argument must be obtained by calling staticMethodId.
Invokes a static method on a Java object, according to the specified method ID. The methodId argument must be obtained by calling staticMethodId.
Invokes a static method on a Java object, according to the specified method ID. The methodId argument must be obtained by calling staticMethodId.
fun JClass.callStaticDoubleMethod(methodId: JMethodID, args: JArguments): Double
Invokes a static method on a Java object, according to the specified method ID. The methodId argument must be obtained by calling staticMethodId.
Invokes a static method on a Java object, according to the specified method ID. The methodId argument must be obtained by calling staticMethodId.
Invokes a static method on a Java object, according to the specified method ID. The methodId argument must be obtained by calling staticMethodId.
Invokes a static method on a Java object, according to the specified method ID. The methodId argument must be obtained by calling staticMethodId.
inline fun <R> JClass.callStaticMethod(methodId: JMethodID, args: JArguments): R
Unifies usage of callStatic<type>Method methods, returning method with the appropriate type wrapped in lambda.
fun JClass.callStaticObjectMethod(methodId: JMethodID, args: JArguments): JObject?
Invokes a static method on a Java object, according to the specified method ID. The methodId argument must be obtained by calling staticMethodId.
Invokes a static method on a Java object, according to the specified method ID. The methodId argument must be obtained by calling staticMethodId.
Invokes a static method on a Java object, according to the specified method ID. The methodId argument must be obtained by calling staticMethodId.
Add char value to arguments.
Clears any exception that is currently being thrown. If no exception is currently being thrown, this routine has no effect.
inline fun JByteArray.copyInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = this.length): ByteArray
Copies a region of this JVM byte[] into destination using getRegion and returns destination.
Loads and initializes a Java VM. The current thread becomes the main thread.
inline fun defineClass(name: String, loader: JObject?, classBuf: CPointer<ByteVar>, classBufLen: Int): JClass
Alias for defineClass which accepts the class name as a Kotlin String.
fun defineClass(name: CValuesRef<ByteVar>?, loader: JObject?, classBuf: CPointer<ByteVar>, classBufLen: Int): JClass?
Loads a class from a classBuf of raw class data.
Deletes the global reference pointed to by the receiver.
Deletes the local reference pointed to by the receiver.
Delete the VM resources needed for the given weak global reference.
Detaches the current thread from a Java VM. All Java monitors held by this thread are released. All Java threads waiting for this thread to die are notified.
Ensures that at least a given number of local references can be created in the current thread.
Alias for fatalError which accepts the message as a Kotlin String.
fun fatalError(message: CValuesRef<ByteVar>?): Nothing
Raises a fatal error and does not expect the VM to recover.
inline fun JClass.fieldId(name: String, sig: String): JFieldID
Alias for JClass.fieldId which accepts the field name and sig as Kotlin Strings.
fun JClass.fieldId(name: CValuesRef<ByteVar>, sig: CValuesRef<ByteVar>): JFieldID?
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.
In JDK release 1.1, 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.
Add float value to arguments.
Converts a java.lang.reflect.Field to a field ID.
Converts a java.lang.reflect.Method or java.lang.reflect.Constructor object to a method ID.
fun JString.getChars(): Pair<CArrayPointer<UShortVar>, Boolean>?
Returns a pointer to the array of Unicode characters and a boolean value isCopy which specifies whether the array is a copy (true - a copy, `false - the underlying array of the string, which means that any changes to it will be reflected on the original string).
fun JString.getCharsCritical(): Pair<CArrayPointer<UShortVar>, Boolean>?
The semantics of this function is similar to getChars function. If possible, the VM returns a pointer to string elements; otherwise, a copy is made. However, there are significant restrictions on how these functions can be used. In a code segment enclosed by getCharsCritical/releaseCharsCritical calls, the native code must not issue arbitrary JNI calls, or cause the current thread to block.
fun JBooleanArray.getElements(): Pair<CArrayPointer<UByteVar>, Boolean>?
fun JByteArray.getElements(): Pair<CArrayPointer<ByteVar>, Boolean>?
fun JCharArray.getElements(): Pair<CArrayPointer<UShortVar>, Boolean>?
fun JDoubleArray.getElements(): Pair<CArrayPointer<DoubleVar>, Boolean>?
fun JFloatArray.getElements(): Pair<CArrayPointer<FloatVar>, Boolean>?
fun JIntArray.getElements(): Pair<CArrayPointer<IntVar>, Boolean>?
fun JLongArray.getElements(): Pair<CArrayPointer<LongVar>, Boolean>?
fun JShortArray.getElements(): Pair<CArrayPointer<ShortVar>, Boolean>?
Returns the body of the primitive array and a boolean value isCopy which specifies whether the array is a copy (true - a copy, `false - the real array body, which means that any changes to it will be reflected on the jvm array).
fun <T : CPrimitiveVar> JPrimitiveArray<T>.getElementsCritical(): Pair<CArrayPointer<T>, Boolean>?
The semantics of this function is very similar to the getElements. If possible, the VM returns a pointer to the primitive array; otherwise, a copy is made. However, there are significant restrictions on how these functions can be used.
If the current thread is not attached to the VM or the specified version is not supported, throw an exception. Otherwise, returns JniEnv.
Copies a region of a boolean[] array into a buffer buf.
Copies a region of a byte[] array into a buffer buf.
Copies a region of a char[] array into a buffer buf.
Copies a region of a double[] array into a buffer buf.
Copies a region of a float[] array into a buffer buf.
Copies a region of a int[] array into a buffer buf.
Copies a region of a long[] array into a buffer buf.
Copies a region of a short[] array into a buffer buf.
Copies len number of Unicode characters beginning at offset start to the given buffer buf.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
fun JString.getUTFChars(): Pair<CArrayPointer<ByteVar>, Boolean>?
Returns a pointer to an array of bytes representing the string in modified UTF-8 encoding and a boolean value isCopy which specifies whether the array is a copy (true - a copy, `false - the underlying array of the string, which means that any changes to it will be reflected on the original string).
Translates len number of Unicode characters beginning at offset start into modified UTF-8 encoding and place the result in the given buffer buf.
Convenient way to write
Tests whether an object is an instance of a class.
Add int value to arguments.
Tests whether an object is a virtual Thread.
inline fun jArgs(count: Int, block: JArgumentsBuilder.() -> Unit): JArguments
Allocates and initializes JArguments.
Add long value to arguments.
inline fun JClass.methodId(name: String, sig: String): JMethodID
Alias for JClass.methodId which accepts the method name and sig as Kotlin Strings.
fun JClass.methodId(name: CValuesRef<ByteVar>, sig: CValuesRef<ByteVar>): JMethodID?
Returns the method ID for an instance (nonstatic) method of a class or interface. The method may be defined in one of the this@GetMethodID’s supertypes and inherited by this@GetMethodID. The method is determined by its name and signature.
Enters the monitor associated with the underlying Java object referred to by 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.
Constructs a new boolean[] array object.
Constructs a new byte[] array object.
Constructs a new char[] array object.
Allocates and returns a direct java.nio.ByteBuffer referring to the block of memory starting at the memory address address and extending capacity bytes. The byte order of the returned buffer is always big-endian (high byte first; java.nio.ByteOrder.BIG_ENDIAN).
Constructs a new double[] array object.
Constructs a new float[] array object.
Constructs a new int[] array object.
Constructs a new long[] array object.
fun newObjectArray(length: Int, elementClass: JClass, initialElement: JObject? = null): JObjectArray?
Constructs a new array holding objects in class elementClass. All elements are initially set to initialElement.
Constructs a new short[] array object.
Constructs a new java.lang.String object from an array of characters in modified UTF-8 encoding.
inline fun JavaVMInitArgsBuilder.option(optionString: String, extraInfo: COpaquePointer? = null)
Add optionString and extraInfo pair to JavaVMInitArgs.
Pops off the current local reference frame and frees all the local references.
Pops off the current local reference frame, frees all the local references and returns a local reference in the previous local reference frame for the given result object.
Prints an exception and a backtrace of the stack to a system error-reporting channel, such as stderr.
Creates a new local reference frame, in which at least a given number of local references can be created.
Add Object value to arguments.
inline fun JNINativeMethodRegistry.register(name: String, signature: String, functionPointer: JRef<CFunction<*>>)
Alias for JNINativeMethodRegistry.register which accepts name and signature as Kotlin Strings.
inline fun JNINativeMethodRegistry.register(name: CValuesRef<ByteVar>, signature: CValuesRef<ByteVar>, functionPointer: JRef<CFunction<*>>)
Register new JniNativeMethod with specified name, signature and functionPointer.
inline fun JClass.registerNatives(count: Int, block: JNINativeMethodRegistry.() -> Unit)
Registers native methods with the class specified by the clazz argument.
fun JClass.registerNatives(methods: CArrayPointer<JniNativeMethod>, methodsCount: Int)
Registers native methods with the class specified by the receiver. The methods parameter specifies a list of triples that contain the names, signatures and function pointers of the native methods.
The semantics of this function is similar to releaseChars function. Informs the VM that the native code no longer needs access to carray. However, there are significant restrictions on how these functions can be used. In a code segment enclosed by getCharsCritical/releaseCharsCritical calls, the native code must not issue arbitrary JNI calls, or cause the current thread to block.
fun JBooleanArray.releaseElements(elems: CArrayPointer<UByteVar>, mode: ApplyChangesMode)
fun JByteArray.releaseElements(elems: CArrayPointer<ByteVar>, mode: ApplyChangesMode)
fun JCharArray.releaseElements(elems: CArrayPointer<UShortVar>, mode: ApplyChangesMode)
fun JDoubleArray.releaseElements(elems: CPointer<DoubleVar>, mode: ApplyChangesMode)
fun JFloatArray.releaseElements(elems: CPointer<FloatVar>, mode: ApplyChangesMode)
fun JLongArray.releaseElements(elems: CPointer<LongVar>, mode: ApplyChangesMode)
fun JShortArray.releaseElements(elems: CArrayPointer<ShortVar>, mode: ApplyChangesMode)
Informs the VM that the native code no longer needs access to elems. The elems argument is a pointer derived from the receiver using the getElements function. If necessary, this function copies back all changes made to elems to the original array.
fun <T : CPrimitiveVar> JPrimitiveArray<T>.releaseElementsCritical(carray: CArrayPointer<T>, mode: ApplyChangesMode)
The semantics of this function is very similar to the releaseElements.
Informs the VM that the native code no longer needs access to utf. The utf argument is a pointer derived from string using getUTFChars.
Retrieves the default initialization arguments for the Java VM.
Copies back a region of a boolean[] array from a buffer buf.
Copies back a region of a byte[] array from a buffer buf.
Copies back a region of a char[] array from a buffer buf.
Copies back a region of a double[] array from a buffer buf.
Copies back a region of a float[] array from a buffer buf.
Copies back a region of a int[] array from a buffer buf.
Copies back a region of a long[] array from a buffer buf.
Copies back a region of a short[] array from a buffer buf.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
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 staticFieldId.
Add short value to arguments.
inline fun JClass.staticFieldId(name: String, sig: String): JFieldID
Alias for JClass.staticFieldId which accepts the field name and sig as Kotlin Strings.
fun JClass.staticFieldId(name: CValuesRef<ByteVar>, sig: CValuesRef<ByteVar>): JFieldID?
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.
inline fun JClass.staticMethodId(name: String, sig: String): JMethodID
Alias for JClass.staticMethodId which accepts the method name and sig as Kotlin Strings.
fun JClass.staticMethodId(name: CValuesRef<ByteVar>, sig: CValuesRef<ByteVar>): JMethodID?
Returns the method ID for a static method of a class. The method is specified by its name and signature.
Causes a java.lang.Throwable object to be thrown.
inline fun throwNew(clazz: JClass, message: String)
fun throwNew(clazz: JClass, message: CValuesRef<ByteVar>?)
Converts Kotlin DoubleArray into JVM double[] using setRegion.
Converts Kotlin FloatArray into JVM float[] using setRegion.
Converts Kotlin ShortArray into JVM short[] using setRegion.
Converts Kotlin UByteArray into JVM boolean[] using setRegion.
Converts Kotlin UShortArray into JVM char[] using setRegion.
Converts Kotlin boolean to JVM boolean.
Converts JVM boolean[] into Kotlin UByteArray using getRegion.
Converts JVM char[] into Kotlin UShortArray using getRegion.
Converts JVM double[] into Kotlin DoubleArray using getRegion.
Converts JVM float[] into Kotlin FloatArray using getRegion.
Converts JVM short[] into Kotlin ShortArray using getRegion.
Converts JVM boolean to Kotlin boolean.
Unregisters native methods of a receiver class. The class goes back to the state before it was linked or registered with its native method functions.
Alias for memScoped { vm.withEnvAttaching(version) { /* your code */ } } allowing to write less boilerplate code.
inline fun <T> JavaVM.withEnv(version: JniVersion, block: context(JniEnv) () -> T): T
Executes a block of code within the context of a JNI environment of version version of the current thread. If the current thread is not attached to the JavaVM or the specified version is not supported, an exception will be thrown.
inline fun <T> JavaVM.withEnvAttaching(version: JniVersion, name: String, group: JObject? = null, block: context(JniEnv) () -> T): T
Alias for withEnvAttaching which accepts the thread name as a Kotlin String encoded as modified UTF-8 with String.modifiedUtf8.
inline fun <T> JavaVM.withEnvAttaching(version: JniVersion, name: CValuesRef<ByteVar>? = null, group: JObject? = null, block: context(JniEnv) () -> T): T
Attaches the current thread to the Java VM, executes the provided block of code within the attached environment and then detaches the thread upon completion.