Crate javavm[][src]

Expand description

This crate helps “store” and “retrieve” the current Java Virtual Machine in a safe way, plus it provides routines to help you get the JNIEnv for the current thread you’re running on. This way, you set the JVM once and ask for the JNIEnv you need when you need it. Attaching and detaching from the JavaVM should be left to the library, it is none of your business :) Note that you have to set the JavaVM once before you use the functions in this library. Failure to do that will make your program panic!

Example

javavm::set_jvm(None); // Pass a valid JavaVM instance here (hint: use the jni crate, which this crate already depends on)
// ... Other code goes here

let handle = std::thread::spawn(|| {
    // When you need the JNIEnv
    let _ = javavm::get_env();
});

Functions

Cache this class. You can retrieve the class with the load_class_cached function, passing it the name given to this function. Note that this class will only be available for the “current thread”. I do not know if the class instances are usable on multiple threads and until I’m sure about this, thread-local storage is what I’ll stick to

Retrieves the current JNIEnv from the JavaVM.

Retrieves the current JNIEnv from the JavaVM. Does not panic if there is no JavaVM currently set and returns None instead

Retrieve the current JVM as set by set_jvm

Find and cache the JClass for the current thread. If the class has already been looked up, it returns the class and avoids any other expensive lookup without any other work on your part. The class will be automatically unloaded on program termination. If you want to unload a class before program termination, use unload_cached_class(&str)

Sets the current JavaVM. All JNIEnv instances will come from this JavaVM

Unloads a cached class (if one exists). Does nothing if the class has not already been cached