JniEnv

Struct JniEnv 

Source
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.

JNI documentation

§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>

Source

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.

Source

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.

Source

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.

Source

pub fn version(&self) -> JniVersion

Get JNI version.

JNI documentation

Trait Implementations§

Source§

impl<'vm> Debug for JniEnv<'vm>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'vm> !Freeze for JniEnv<'vm>

§

impl<'vm> !RefUnwindSafe for JniEnv<'vm>

§

impl<'vm> !Send for JniEnv<'vm>

§

impl<'vm> !Sync for JniEnv<'vm>

§

impl<'vm> Unpin for JniEnv<'vm>

§

impl<'vm> UnwindSafe for JniEnv<'vm>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.