Crates.io | jerk |
lib.rs | jerk |
version | 0.2.3 |
source | src |
created_at | 2019-12-21 06:54:19.185628 |
updated_at | 2022-05-03 02:56:38.636406 |
description | Java Embedding Rust Kit |
homepage | |
repository | https://github.com/MaulingMonkey/jerk |
max_upload_size | |
id | 191156 |
size | 65,125 |
Libraries to compile/embed/test Java alongside a Rust library/application. Similar to cc, but for Java. This is not an official project of Google, Oracle, Sun Microsystems, or anyone else.
Branch | Badges | Notes |
---|---|---|
publish | Stable/published version | |
master | "Completed" stuff that hasn't been published. | |
wip/* | "Work In Progress" - incomplete, use at your own risk. | |
dead/* | Abandoned threads of work |
javac
, jar
, etc.kotlinc
etc.Install the JDK if you haven't already.
Add this to your Cargo.toml:
[lib]
crate-type = ["rlib", "dylib"]
[dependencies]
jni-sys = "0.3"
[build-dependencies]
jerk = "0.2"
[dev-dependencies]
jerk = "0.2"
And this to your build.rs:
fn main() {
jerk::metabuild();
}
You can then write Java (src/Adder.java) code:
package com.maulingmonkey.jerk.example_hello_world_jar;
public class Adder {
public native int add(int a, int b);
public static void test() {
System.loadLibrary("example_hello_world_jar");
assert adder.add(1, 2) == 3;
}
}
...alongside your Rust (src/Adder.rs) code:
use jni_sys::{JNIEnv, jobject, jint};
#[no_mangle] pub extern "stdcall" fn Java_com_maulingmonkey_jerk_example_1hello_1world_1jar_Adder_add__II(_env: *mut JNIEnv, _this: jobject, a: jint, b: jint) -> jint {
a + b
}
...and write Java integration tests (tests/test.rs):
#[test] fn test() {
jerk::run_test!("com.maulingmonkey.jerk.example_hello_world_jar", "Adder", "test");
}
...and then build and run the test!
C:\local\jerk>cargo t
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
Running target\debug\deps\example_hello_world_jar-2997df28c387b743.exe
running 1 tests
test adder::test ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Rust code may sanely depend on Java code to build, but not vicea versa:
You can still have:
native
methods declared in Java.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.