Dushistov / flapigen-rs

Tool for connecting programs or libraries written in Rust with other languages
BSD 3-Clause "New" or "Revised" License
775 stars 59 forks source link

Access JNIEnv #463

Closed Dreaming-Codes closed 1 month ago

Dreaming-Codes commented 1 month ago

Is there any way to access JNIEnv? I'm asking this since I would like to use https://crates.io/crates/log4j

Dushistov commented 1 month ago

Is there any way to access JNIEnv?

Yes.

You can get full access to Java/JNI interface via foreigner_code, see appropriate section in documentation.

Or you can use implicit env variable in "inline" methods:

foreign_class!(
    class LoggingInit {
        fn init() {
            println!("Here is env: {env:p}");
        }
    }
);

flapigen generates something like this:

#[allow(non_snake_case, unused_variables, unused_mut, unused_unsafe)]
#[no_mangle]
pub extern "C" fn Java_com_example_rust_LoggingInit_init(env: *mut JNIEnv, _: jclass) -> () {
    let mut ret: () = {
        println!("Here is env: {env:p}");
    };
    ret
}
Dreaming-Codes commented 1 month ago

Thanks