[][src]Crate jnix

This crate provides high-level extensions to help with the usage of JNI in Rust code. Internally, it uses the jni-rs crate for the low-level JNI operations.

Some helper traits are provided, such as:

  • AsJValue: for allowing a JNI type to be convected to a JValue wrapper type.
  • IntoJava: for allowing a Rust type to be converted to a Java type.

A JnixEnv helper type is also provided, which is a JNIEnv wrapper that contains an internal class cache for preloaded classes.

If compiled with the derive feature flag, the crate also exports a derive procedural macro for IntoJava, which allows writing conversion code a lot easier. An example would be:

use jnix::{
    jni::{objects::JObject, JNIEnv},
    JnixEnv, IntoJava,

// Rust type definition
#[derive(Default, IntoJava)]
#[jnix(package = "my.package")]
pub struct MyData {
    number: i32,
    string: String,

// A JNI function called from Java that creates a `MyData` Rust type, converts it to a Java
// type and returns it.
pub extern "system" fn Java_my_package_JniClass_getData<'env>(
    env: JNIEnv<'env>,
    _this: JObject<'env>,
) -> JObject<'env> {
    // Create the `JnixEnv` wrapper
    let env = JnixEnv::from(env);

    // Prepare the result type
    let data = MyData::default();

    // Since a smart pointer is returned from `into_java`, the inner object must be "leaked" so
    // that the garbage collector can own it afterwards
package my.package;

public class MyData {
    public MyData(int number, String string) {
        // This is the constructor that is called by the generated `IntoJava` code
        // Note that the fields don't actually have to exist, the only thing that's necessary
        // is for the target Java class to have a constructor with the expected type signature
        // following the field order of the Rust type.


pub extern crate jni;



Provides access to JNI functions.



Returns a value as its JValue representation.


Conversion from a Java type into its Rust equivalent.


Conversion from a type into its Java representation.