pub struct JavaVM { /* private fields */ }Expand description
A struct for interacting with the Java VM.
§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
 
impl JavaVM
Sourcepub fn create(arguments: &InitArguments) -> Result<Self, JniError>
 
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.
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 fn attach(
    &self,
    arguments: &AttachArguments<'_>,
) -> Result<JniEnv<'_>, JniError>
 
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.
Sourcepub fn attach_daemon(
    &self,
    arguments: &AttachArguments<'_>,
) -> Result<JniEnv<'_>, JniError>
 
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.
Trait Implementations§
impl Send for JavaVM
Make JavaVM sendable between threads. Guaranteed to be safe by JNI.
impl Sync for JavaVM
Make JavaVM shareable by multiple threads. Guaranteed to be safe
by JNI.