pub struct JavaVM { /* private fields */ }
Implementations§
Source§impl JavaVM
impl JavaVM
Sourcepub unsafe fn AttachCurrentThread_str(
&self,
version: jint,
thread_name: impl UseCString,
thread_group: jobject,
) -> Result<JNIEnv, jint>
pub unsafe fn AttachCurrentThread_str( &self, version: jint, thread_name: impl UseCString, thread_group: jobject, ) -> Result<JNIEnv, jint>
Attaches the current thread to the JVM as a normal thread. If a thread name is provided then it will be used as the java name of the current thread.
§Errors
JNI implementation specific error constants like JNI_EINVAL
§Safety
This fn must not be called on a JavaVM
object that has been destroyed or is in the process of being destroyed.
Sourcepub unsafe fn AttachCurrentThread(
&self,
args: *mut JavaVMAttachArgs,
) -> Result<JNIEnv, jint>
pub unsafe fn AttachCurrentThread( &self, args: *mut JavaVMAttachArgs, ) -> Result<JNIEnv, jint>
Attaches the current thread to the JVM as a normal thread. If a thread name is provided then it will be used as the java name of the current thread.
§Errors
JNI implementation specific error constants like JNI_EINVAL
§Panics
If the JVM does not return an error but also does not set the JNIEnv
ptr.
§Safety
This fn must not be called on a JavaVM
object that has been destroyed or is in the process of being destroyed.
Sourcepub unsafe fn AttachCurrentThreadAsDaemon_str(
&self,
version: jint,
thread_name: impl UseCString,
thread_group: jobject,
) -> Result<JNIEnv, jint>
pub unsafe fn AttachCurrentThreadAsDaemon_str( &self, version: jint, thread_name: impl UseCString, thread_group: jobject, ) -> Result<JNIEnv, jint>
Attaches the current thread to the JVM as a daemon thread. If a thread name is provided then it will be used as the java name of the current thread.
§Errors
JNI implementation specific error constants like JNI_EINVAL
§Safety
This fn must not be called on a JavaVM
object that has been destroyed or is in the process of being destroyed.
Sourcepub unsafe fn AttachCurrentThreadAsDaemon(
&self,
args: *mut JavaVMAttachArgs,
) -> Result<JNIEnv, jint>
pub unsafe fn AttachCurrentThreadAsDaemon( &self, args: *mut JavaVMAttachArgs, ) -> Result<JNIEnv, jint>
Attaches the current thread to the JVM as a daemon thread. If a thread name is provided then it will be used as the java name of the current thread.
§Errors
JNI implementation specific error constants like JNI_EINVAL
§Panics
If the JVM does not return an error but also does not set the JNIEnv
ptr.
§Safety
This fn must not be called on a JavaVM
object that has been destroyed or is in the process of being destroyed.
Sourcepub unsafe fn GetEnv<T: SealedEnvVTable>(
&self,
jni_version: jint,
) -> Result<T, jint>
pub unsafe fn GetEnv<T: SealedEnvVTable>( &self, jni_version: jint, ) -> Result<T, jint>
Gets the JNIEnv
for the current thread.
Concerning the generic type T
. This type must refer to the correct function table for the given jni_version
:
- For ordinary
jni_version
valuesT
must beJNIEnv
. - For jvmti
jni_version
valuesT
must beJVMTIEnv
. *mut c_void
is also always a valid type forT
regardless of the value ofjni_version
!- using
*mut c_void
will return the raw function table.
Using the wrong type for T
is undefined behavior!
There is no way to check this as jvmti and jni function tables are completely different!
§Safety
This fn must not be called on a JavaVM
object that has been destroyed or is in the process of being destroyed.
§Panics
If the JVM does not return an error but also does not set the JNIEnv
ptr.
If the asserts feature is enabled and the implementation can detect that T
is not correct.
This is only provided on a best effort basis.
§Errors
JNI implementation specific error constants like JNI_EINVAL
§Undefined behavior
Using the wrong type T
for the given jni_version
. I.e. using JNIEnv
for JVMTI
or JVMTIEnv
for JNI
.
§Example
use std::ffi::c_void;
use jni_simple::{JNIEnv, JVMTIEnv, JavaVM, JNI_VERSION_1_8, JVMTI_VERSION_21};
unsafe fn some_func(vm: &JavaVM) {
//for 99% use cases this is what you want!
let jni = vm.GetEnv::<JNIEnv>(JNI_VERSION_1_8).expect("Error");
let jni_raw = vm.GetEnv::<*mut c_void>(JNI_VERSION_1_8).expect("Error");
let jvmti = vm.GetEnv::<JVMTIEnv>(JVMTI_VERSION_21).expect("Error");
let jni_raw = vm.GetEnv::<*mut c_void>(JVMTI_VERSION_21).expect("Error");
}
Sourcepub unsafe fn DetachCurrentThread(&self) -> jint
pub unsafe fn DetachCurrentThread(&self) -> jint
Detaches the current thread from the jvm.
This should only be called on functions that were attached with AttachCurrentThread
or AttachCurrentThreadAsDaemon
.
§Safety
Detaches the current thread. The JNIEnv
of the current thread is no longer valid after this call.
Any further calls made using it will result in undefined behavior.
Sourcepub unsafe fn DestroyJavaVM(&self)
pub unsafe fn DestroyJavaVM(&self)
This function will block until all java threads have completed and then destroy the JVM. It should not be called from a method that is called from the JVM.
§Safety
Careful consideration should be taken when this fn is called. As mentioned calling it from
a JVM Thread will probably just block the calling thread forever. However, this fn also
does stuff internally with the jvm, after/during its return the JVM can no longer be used in
any thread. Any existing JavaVM
object will become invalid. Attempts to obtain a JNIEnv
after
this fn returns by way of calling AttachThread
will likely lead to undefined behavior.
Shutting down a JVM is a “terminal” operation for any Hotspot implementation of the JVM.
The current process will never be able to relaunch a hotspot JVM.
This fn should therefore only be used if a rust thread needs to “wait” until the JVM is dead to then perform
some operations such a cleanup before eventually calling exit()
Please note that this fn never returns if the JavaVM
terminates abnormally (e.g. due to a crash),
or someone calling Runtime.getRuntime().halt(…) in Java, because that just terminates the Process instantly.
Its usefulness to run shutdown code is therefore limited.