#coap #iot #networking #no-std #wasm


JNI abstractions and bindings used by the toad ecosystem

24 releases (14 breaking)

1.0.0 May 22, 2023
0.16.1 Apr 26, 2023

#3 in #coap-server

Download history 22/week @ 2023-06-09 46/week @ 2023-06-16 66/week @ 2023-06-30 44/week @ 2023-07-07 13/week @ 2023-07-14 21/week @ 2023-07-21 3/week @ 2023-07-28 50/week @ 2023-08-04 22/week @ 2023-08-18 22/week @ 2023-08-25 3/week @ 2023-09-01 26/week @ 2023-09-08 25/week @ 2023-09-15 5/week @ 2023-09-22

59 downloads per month


14K SLoC

crates.io docs.rs Maintenance


High-level wrapper of [jni], making Java & Rust FFI easy & fun


toad_jni::global offers the option to use a global JVM handle (toad_jni::global::jvm() set with toad_jni::global::init()).

Using the JVM global is completely optional, unless you plan to use Rust trait impls such as IntoIterator on toad_jni::java::util::ArrayList.


All java type signatures can be represented by rust types that implement the toad_jni::java::Type trait, which is automatically implemented for all toad_jni::java::Classes.


Classes are represented in toad_jni by implementing 2 traits:

Fields and Methods

There are several high-level lens-style structs for interacting with fields, methods and constructors:

All of these types use toad_jni::java::Type to transform nice Rust types into the corresponding JVM type signatures.

For example, the StaticMethod representation of java.lang.String.format(String, ..Object) would be:

use toad_jni::java::lang::Object;
use toad_jni::java::StaticMethod;

static STRING_FORMAT: StaticMethod<String, fn(String, Vec<Object>) -> String> =

It is recommended that these structs are stored in local static variables so that they can cache the internal JNI IDs of the class and methods, but this is not required.


Consider the following java class:

package com.foo.bar;

public class Foo {
  public final static long NUMBER = 123;
  public String bingus = "bingus";

  public Foo() { }

  public static String bar() {
    return "bar";

  public void setBingus(String newBingus) {
    this.bingus = newBingus;

A Rust API to this class would look like:

use toad_jni::java;

pub struct Foo(java::lang::Object);


impl java::Class for Foo {
  const PATH: &'static str = "com/foo/bar/Foo";

impl Foo {
  pub fn new(e: &mut java::Env) -> Self {
    static CTOR: java::Constructor<Foo, fn()> = java::Constructor::new();

  pub fn number(e: &mut java::Env) -> i64 {
    static NUMBER: java::StaticField<Foo, i64> = java::StaticField::new("NUMBER");

  pub fn bar(e: &mut java::Env) -> String {
    static BAR: java::StaticMethod<Foo, fn() -> String> = java::StaticMethod::new("bar");

  pub fn bingus(&self, e: &mut java::Env) -> String {
    static BINGUS: java::Field<Foo, String> = java::Field::new("bingus");
    BINGUS.get(e, self)

  pub fn set_bingus(&self, e: &mut java::Env, s: String) {
    static SET_BINGUS: java::Method<Foo, fn(String)> = java::Method::new("setBingus");
    SET_BINGUS.invoke(e, self, s)


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~229K SLoC