JavaVM

Struct JavaVM 

Source
pub struct JavaVM { /* private fields */ }

Implementations§

Source§

impl JavaVM

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 values T must be JNIEnv.
  • For jvmti jni_version values T must be JVMTIEnv.
  • *mut c_void is also always a valid type for T regardless of the value of jni_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");
}
Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for JavaVM

Source§

fn clone(&self) -> JavaVM

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JavaVM

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Copy for JavaVM

Auto Trait Implementations§

§

impl Freeze for JavaVM

§

impl RefUnwindSafe for JavaVM

§

impl Send for JavaVM

§

impl Sync for JavaVM

§

impl Unpin for JavaVM

§

impl UnwindSafe for JavaVM

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.