#java #jni

javavm

A simple crate that caches the JavaVM structure globally and returns a valid JNIEnv for any thread, providing automatic thread attachment and detachment

3 releases

0.1.2 Oct 24, 2021
0.1.1 Oct 23, 2021
0.1.0 Oct 23, 2021

#68 in #jni


Used in mediacodec

MIT/Apache

6KB
82 lines

JavaVM

A simple crate that caches the JavaVM structure globally and returns a valid JNIEnv for any thread, providing automatic thread attachment and detachment.

See the crate documentation for more details.


lib.rs:

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();
});

Dependencies

~1.2–2.6MB
~43K SLoC