add jni
This commit is contained in:
18
jni/HelloWorld.java
Normal file
18
jni/HelloWorld.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
public class HelloWorld {
|
||||||
|
// This declares that the static `hello` method will be provided
|
||||||
|
// a native library.
|
||||||
|
private static native String hello(String input);
|
||||||
|
|
||||||
|
static {
|
||||||
|
// This actually loads the shared object that we'll be creating.
|
||||||
|
// The actual location of the .so or .dll may differ based on your
|
||||||
|
// platform.
|
||||||
|
System.loadLibrary("samplejni");
|
||||||
|
}
|
||||||
|
|
||||||
|
// The rest is just regular ol' Java!
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String output = HelloWorld.hello("josh");
|
||||||
|
System.out.println(output);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
jni/README.md
Normal file
13
jni/README.md
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
> https://docs.rs/jni/0.13.0/jni/
|
||||||
|
>
|
||||||
|
> https://crates.io/crates/jni
|
||||||
|
|
||||||
|
Run:
|
||||||
|
```bash
|
||||||
|
cd samplejni; cargo build
|
||||||
|
cd -; javac HelloWorld.java
|
||||||
|
LD_LIBRARY_PATH=samplejni/target/debug/ java HelloWorld
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
11
jni/samplejni/Cargo.toml
Normal file
11
jni/samplejni/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "samplejni"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Hatter Jiang@Pixelbook <jht5945@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
jni = "0.13.0"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
crate_type = ["cdylib"]
|
||||||
42
jni/samplejni/src/lib.rs
Normal file
42
jni/samplejni/src/lib.rs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
extern crate jni;
|
||||||
|
|
||||||
|
// This is the interface to the JVM that we'll call the majority of our
|
||||||
|
// methods on.
|
||||||
|
use jni::JNIEnv;
|
||||||
|
|
||||||
|
// These objects are what you should use as arguments to your native
|
||||||
|
// function. They carry extra lifetime information to prevent them escaping
|
||||||
|
// this context and getting used after being GC'd.
|
||||||
|
use jni::objects::{JClass, JString};
|
||||||
|
|
||||||
|
// This is just a pointer. We'll be returning it from our function. We
|
||||||
|
// can't return one of the objects with lifetime information because the
|
||||||
|
// lifetime checker won't let us.
|
||||||
|
use jni::sys::jstring;
|
||||||
|
|
||||||
|
// This keeps Rust from "mangling" the name and making it unique for this
|
||||||
|
// crate.
|
||||||
|
#[no_mangle]
|
||||||
|
// This turns off linter warnings because the name doesn't conform to
|
||||||
|
// conventions.
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
pub extern "system" fn Java_HelloWorld_hello(env: JNIEnv,
|
||||||
|
// This is the class that owns our static method. It's not going to be used,
|
||||||
|
// but still must be present to match the expected signature of a static
|
||||||
|
// native method.
|
||||||
|
class: JClass,
|
||||||
|
input: JString)
|
||||||
|
-> jstring {
|
||||||
|
// First, we have to get the string out of Java. Check out the `strings`
|
||||||
|
// module for more info on how this works.
|
||||||
|
let input: String =
|
||||||
|
env.get_string(input).expect("Couldn't get java string!").into();
|
||||||
|
|
||||||
|
// Then we have to create a new Java string to return. Again, more info
|
||||||
|
// in the `strings` module.
|
||||||
|
let output = env.new_string(format!("Hello, {}!", input))
|
||||||
|
.expect("Couldn't create java string!");
|
||||||
|
|
||||||
|
// Finally, extract the raw pointer to return.
|
||||||
|
output.into_inner()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user