richie
5 years ago
commit
e0c23ca4b9
6 changed files with 277 additions and 0 deletions
@ -0,0 +1,13 @@ |
|||||||
|
class HelloWorld { |
||||||
|
|
||||||
|
private static native String hello(String input); |
||||||
|
static { |
||||||
|
String dir = System.getProperty("user.dir"); |
||||||
|
System.load(dir + "/native/mylib/target/debug/libmylib.dylib"); |
||||||
|
} |
||||||
|
|
||||||
|
public static void main(String[] args) { |
||||||
|
String output = HelloWorld.hello("fanruan"); |
||||||
|
System.out.println(output); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,210 @@ |
|||||||
|
# This file is automatically @generated by Cargo. |
||||||
|
# It is not intended for manual editing. |
||||||
|
[[package]] |
||||||
|
name = "ascii" |
||||||
|
version = "0.9.2" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "backtrace" |
||||||
|
version = "0.3.33" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
dependencies = [ |
||||||
|
"backtrace-sys 0.1.31 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"cfg-if 0.1.9 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"libc 0.2.60 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"rustc-demangle 0.1.15 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "backtrace-sys" |
||||||
|
version = "0.1.31" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
dependencies = [ |
||||||
|
"cc 1.0.37 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"libc 0.2.60 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "byteorder" |
||||||
|
version = "1.3.2" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "cc" |
||||||
|
version = "1.0.37" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "cesu8" |
||||||
|
version = "1.1.0" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "cfg-if" |
||||||
|
version = "0.1.9" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "combine" |
||||||
|
version = "3.8.1" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
dependencies = [ |
||||||
|
"ascii 0.9.2 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"byteorder 1.3.2 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"either 1.5.2 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"memchr 2.2.1 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"unreachable 1.0.0 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "either" |
||||||
|
version = "1.5.2" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "error-chain" |
||||||
|
version = "0.12.1" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
dependencies = [ |
||||||
|
"backtrace 0.3.33 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"version_check 0.1.5 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "jni" |
||||||
|
version = "0.13.0" |
||||||
|
source = "git+https://github.com/prevoty/jni-rs#ceeec77366816eec42536fc2ed27c8571a62535e" |
||||||
|
dependencies = [ |
||||||
|
"cesu8 1.1.0 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"combine 3.8.1 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"error-chain 0.12.1 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"jni-sys 0.3.0 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"log 0.4.7 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"walkdir 2.2.9 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "jni-sys" |
||||||
|
version = "0.3.0" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "libc" |
||||||
|
version = "0.2.60" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "log" |
||||||
|
version = "0.4.7" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
dependencies = [ |
||||||
|
"cfg-if 0.1.9 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "memchr" |
||||||
|
version = "2.2.1" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "mylib" |
||||||
|
version = "0.1.0" |
||||||
|
dependencies = [ |
||||||
|
"jni 0.13.0 (git+https://github.com/prevoty/jni-rs)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "rustc-demangle" |
||||||
|
version = "0.1.15" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "same-file" |
||||||
|
version = "1.0.5" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
dependencies = [ |
||||||
|
"winapi-util 0.1.2 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "unreachable" |
||||||
|
version = "1.0.0" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
dependencies = [ |
||||||
|
"void 1.0.2 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "version_check" |
||||||
|
version = "0.1.5" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "void" |
||||||
|
version = "1.0.2" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "walkdir" |
||||||
|
version = "2.2.9" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
dependencies = [ |
||||||
|
"same-file 1.0.5 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"winapi 0.3.7 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"winapi-util 0.1.2 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "winapi" |
||||||
|
version = "0.3.7" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
dependencies = [ |
||||||
|
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "winapi-i686-pc-windows-gnu" |
||||||
|
version = "0.4.0" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "winapi-util" |
||||||
|
version = "0.1.2" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
dependencies = [ |
||||||
|
"winapi 0.3.7 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "winapi-x86_64-pc-windows-gnu" |
||||||
|
version = "0.4.0" |
||||||
|
source = "registry+https://mirrors.ustc.edu.cn/crates.io-index/" |
||||||
|
|
||||||
|
[metadata] |
||||||
|
"checksum ascii 0.9.2 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "91e320562a8fa3286a481b7189f89578ace6b20df99e123c87f2f509c957c5d6" |
||||||
|
"checksum backtrace 0.3.33 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "88fb679bc9af8fa639198790a77f52d345fe13656c08b43afa9424c206b731c6" |
||||||
|
"checksum backtrace-sys 0.1.31 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "82a830b4ef2d1124a711c71d263c5abdc710ef8e907bd508c88be475cebc422b" |
||||||
|
"checksum byteorder 1.3.2 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" |
||||||
|
"checksum cc 1.0.37 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "39f75544d7bbaf57560d2168f28fd649ff9c76153874db88bdbdfd839b1a7e7d" |
||||||
|
"checksum cesu8 1.1.0 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" |
||||||
|
"checksum cfg-if 0.1.9 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" |
||||||
|
"checksum combine 3.8.1 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "da3da6baa321ec19e1cc41d31bf599f00c783d0517095cdaf0332e3fe8d20680" |
||||||
|
"checksum either 1.5.2 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b" |
||||||
|
"checksum error-chain 0.12.1 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "3ab49e9dcb602294bc42f9a7dfc9bc6e936fca4418ea300dbfb84fe16de0b7d9" |
||||||
|
"checksum jni 0.13.0 (git+https://github.com/prevoty/jni-rs)" = "<none>" |
||||||
|
"checksum jni-sys 0.3.0 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" |
||||||
|
"checksum libc 0.2.60 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "d44e80633f007889c7eff624b709ab43c92d708caad982295768a7b13ca3b5eb" |
||||||
|
"checksum log 0.4.7 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "c275b6ad54070ac2d665eef9197db647b32239c9d244bfb6f041a766d00da5b3" |
||||||
|
"checksum memchr 2.2.1 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" |
||||||
|
"checksum rustc-demangle 0.1.15 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "a7f4dccf6f4891ebcc0c39f9b6eb1a83b9bf5d747cb439ec6fba4f3b977038af" |
||||||
|
"checksum same-file 1.0.5 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "585e8ddcedc187886a30fa705c47985c3fa88d06624095856b36ca0b82ff4421" |
||||||
|
"checksum unreachable 1.0.0 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" |
||||||
|
"checksum version_check 0.1.5 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" |
||||||
|
"checksum void 1.0.2 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" |
||||||
|
"checksum walkdir 2.2.9 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "9658c94fa8b940eab2250bd5a457f9c48b748420d71293b165c8cdbe2f55f71e" |
||||||
|
"checksum winapi 0.3.7 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" |
||||||
|
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" |
||||||
|
"checksum winapi-util 0.1.2 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" |
||||||
|
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://mirrors.ustc.edu.cn/crates.io-index/)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" |
@ -0,0 +1,13 @@ |
|||||||
|
[package] |
||||||
|
name = "mylib" |
||||||
|
version = "0.1.0" |
||||||
|
authors = ["richie <richie@fanruan.com>"] |
||||||
|
edition = "2018" |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
jni = { git = "https://github.com/prevoty/jni-rs" } |
||||||
|
|
||||||
|
[lib] |
||||||
|
crate_type = ["dylib"] |
@ -0,0 +1,36 @@ |
|||||||
|
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() |
||||||
|
} |
Loading…
Reference in new issue