#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

#47 in #jni

Download history 3/week @ 2024-02-17 25/week @ 2024-02-24 2/week @ 2024-03-02 6/week @ 2024-03-09 1/week @ 2024-03-16 43/week @ 2024-03-30 10/week @ 2024-04-06

53 downloads per month
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.4–2.8MB
~47K SLoC