pub struct JniEnv<'vm> { /* private fields */ }Expand description
The interface for interacting with Java.
All calls to Java are performed through this interface.
JNI methods can only be called from threads, explicitly attached to the Java VM.
JniEnv represents such a thread.
§Examples
use rust_jni::{AttachArguments, InitArguments, JavaVM, JniEnv, JniVersion};
use std::ptr;
let init_arguments = InitArguments::get_default(JniVersion::V8).unwrap();
let vm = JavaVM::create(&init_arguments).unwrap();
let env = vm.attach(&AttachArguments::new(&init_arguments)).unwrap();
unsafe {
assert_ne!(env.raw_env(), ptr::null_mut());
}JniEnv is
!Send. It means it can’t be passed
between threads:
let env = vm.attach(&AttachArguments::new(&init_arguments)).unwrap();
{
::std::thread::spawn(move || {
unsafe { env.raw_env() }; // doesn't compile!
});
}Instead, you need to attach each new thread to the VM:
use std::sync::Arc;
let init_arguments = InitArguments::get_default(JniVersion::V8).unwrap();
let vm = Arc::new(JavaVM::create(&init_arguments).unwrap());
let env = vm.attach(&AttachArguments::new(&init_arguments)).unwrap();
{
let vm = vm.clone();
::std::thread::spawn(move || {
let env = vm.attach(&AttachArguments::new(&init_arguments)).unwrap();
unsafe {
assert_ne!(env.raw_env(), ptr::null_mut());
}
});
}
unsafe {
assert_ne!(env.raw_env(), ptr::null_mut());
}The thread is automatically detached once the JniEnv is dropped.
JniEnv can’t outlive the parent JavaVM.
This code is not allowed:
let env = {
let init_arguments = InitArguments::get_default(JniVersion::V8).unwrap();
let vm = JavaVM::create(&init_arguments).unwrap();
vm.attach(&AttachArguments::new(&init_arguments)).unwrap() // doesn't compile!
};JniEnv represents a thread, attached to the Java VM. Thus there
can’t be two JniEnv-s per thread.
JavaVM::attach will panic if you attempt to do so:
let env = vm.attach(&AttachArguments::new(&init_arguments)).unwrap();
let env = vm.attach(&AttachArguments::new(&init_arguments)).unwrap(); // panics!Implementations§
Source§impl<'vm> JniEnv<'vm>
impl<'vm> JniEnv<'vm>
Sourcepub unsafe fn raw_jvm(&self) -> *mut JavaVM
pub unsafe fn raw_jvm(&self) -> *mut JavaVM
Get the raw Java VM pointer.
This function provides low-level access to all of JNI and thus is unsafe.
Sourcepub unsafe fn raw_env(&self) -> *mut JNIEnv
pub unsafe fn raw_env(&self) -> *mut JNIEnv
Get the raw JNI environment pointer.
This function provides low-level access to all of JNI and thus is unsafe.
Sourcepub fn token(&self) -> NoException<'_>
pub fn token(&self) -> NoException<'_>
Get a NoException token indicating that there is no pending
exception in this thread.
Read more about tokens in NoException documentation.
Sourcepub fn version(&self) -> JniVersion
pub fn version(&self) -> JniVersion
Get JNI version.
Trait Implementations§
Source§impl<'vm> Drop for JniEnv<'vm>
Drop detaches the current thread from the Java VM.
It’s not safe
to do so with an exception pending, so it panics if this happens.
impl<'vm> Drop for JniEnv<'vm>
Drop detaches the current thread from the Java VM.
It’s not safe
to do so with an exception pending, so it panics if this happens.