Giter Club home page Giter Club logo

jnat's Introduction

jnat

A wrapper around the jni crate

View the documentation

Example

Create a new Cargo lib project with cargo new --lib mylib and add the following to Cargo.toml:

[dependencies]
jnat = [latest version]

[lib]
crate-type = ["cdylib"]

Add the following to src/lib.rs:

use jnat::{
  jnat_macros::jnat,
  jni::{objects::JClass, JNIEnv}, // jni crate, re-exported by jnat
  Class,
  Env,
  Signature,
  Type,
};

jnat!(HelloWorld, caller, (JNIEnv, JClass) -> ());

fn caller(env: JNIEnv, class: JClass) {
  let mut env = Env::new(&env);
  let class = Class::new(&mut env, class);

  class
    .call_static_method("hello", Signature::new(&[], Type::Void), &[])
    .unwrap();
}

Then, run cargo build. Create a new file called HelloWorld.java and add the following:

public class HelloWorld {
  private static native void caller();

  static {
    System.loadLibrary("mylib");
  }

  public static void main(String[] args) {
    HelloWorld.caller();
  }

  public static void hello() {
    System.out.println("Hello, world!");
  }
}

Compile the java file with javac -h . HelloWorld.java. Then, run java -Djava.library.path=path/to/target/debug HelloWorld. You should see Hello, world! printed to the console.

Notes

  • Jnat re-exports jni by default. If you want to use a different version of jni, you can disable either the default features or the jni feature.
  • Jnat exports a macro, jnat::jnat_macros::jnat (seen in the example above), which is used to generate the Java_HelloWorld_caller function. This macro can be disabled by disabling either the default features or the jni-macros feature. Note that the macro keeps the original function to prevent unintuitive behavior (so you can, in your Rust code, call just example() instead of Java_org_example_Class_example() while still allowing Java to call it).

jnat's People

Contributors

shreyasm-dev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.