Struct JavaVM

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

A struct for interacting with the Java VM.

JNI documentation

§Examples

use rust_jni::{InitArguments, JavaVM, JniVersion, JvmOption, JvmVerboseOption};
use std::ptr;

let options = InitArguments::get_default(JniVersion::V8).unwrap()
    .with_option(JvmOption::Verbose(JvmVerboseOption::Gc))
    .with_option(JvmOption::Verbose(JvmVerboseOption::Jni));

let vm = JavaVM::create(&options).unwrap();
unsafe {
    assert_ne!(vm.raw_jvm(), ptr::null_mut());
}

let vms = JavaVM::list().unwrap();
unsafe {
    assert_eq!(vms[0].raw_jvm(), vm.raw_jvm());
}

JavaVM is Send + Sync. It means it can be shared between threads.

use rust_jni::{InitArguments, JavaVM, JniVersion};
use std::ptr;
use std::sync::Arc;

let vm =
    Arc::new(JavaVM::create(&InitArguments::get_default(JniVersion::V8).unwrap()).unwrap());
{
    let vm = vm.clone();
    ::std::thread::spawn(move || {
        unsafe {
            assert_ne!(vm.raw_jvm(), ptr::null_mut());
        }
    });
}
unsafe {
    assert_ne!(vm.raw_jvm(), ptr::null_mut());
}

The main purpose of JavaVM is to attach threads by provisioning JniEnv-s.

Implementations§

Source§

impl JavaVM

Source

pub fn create(arguments: &InitArguments) -> Result<Self, JniError>

Create a Java VM with the specified arguments.

Only one Java VM per process is supported. When called for the second time will return an error. This is the case even if the object is dropped.

JNI documentation

Source

pub fn list() -> Result<Vec<Self>, JniError>

Get a list of created Java VMs.

JNI documentation

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 fn attach( &self, arguments: &AttachArguments<'_>, ) -> Result<JniEnv<'_>, JniError>

Attach the current thread to the Java VM with a specific thread name. Returns a JniEnv instance for this thread.

JNI documentation

Source

pub fn attach_daemon( &self, arguments: &AttachArguments<'_>, ) -> Result<JniEnv<'_>, JniError>

Attach the current thread to the Java VM as a daemon with a specific thread name. Returns a JniEnv instance for this thread.

JNI documentation

Trait Implementations§

Source§

impl Debug for JavaVM

Source§

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

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

impl Drop for JavaVM

Make JavaVM be destroyed when the value is dropped.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for JavaVM

Make JavaVM sendable between threads. Guaranteed to be safe by JNI.

Source§

impl Sync for JavaVM

Make JavaVM shareable by multiple threads. Guaranteed to be safe by JNI.

Auto Trait Implementations§

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.