diff --git a/java_src/me/ehlxr/HelloWorld.java b/java_src/me/ehlxr/HelloWorld.java index 6741274..c0e532b 100644 --- a/java_src/me/ehlxr/HelloWorld.java +++ b/java_src/me/ehlxr/HelloWorld.java @@ -4,13 +4,13 @@ class HelloWorld { private static native String hello(String input); private static native byte[] helloByte(byte[] input); private static native void factAndCallMeBack(int n, HelloWorld callback); - private static native long counterNew(HelloWorld callback); private static native void counterIncrement(long counter_ptr); private static native void counterDestroy(long counter_ptr); - private static native void asyncComputation(HelloWorld callback); + private static native String fetchNameStr(HelloWorld param); + static { // Linux: export LD_LIBRARY_PATH=/Users/ehlxr/Desktop/jni_rs/mylib/target/debug // Mac: export JAVA_LIBRARY_PATH=/Users/ehlxr/Desktop/jni_rs/mylib/target/debug @@ -18,23 +18,41 @@ class HelloWorld { } public static void main(String[] args) { - String output = HelloWorld.hello("Java"); - System.out.println(output); - byte[] outputByte = HelloWorld.helloByte("byte".getBytes()); - System.out.println(outputByte); + // String output = HelloWorld.hello("Java"); + // System.out.println(output); - HelloWorld.factAndCallMeBack(6, new HelloWorld()); + // byte[] outputByte = HelloWorld.helloByte("byte".getBytes()); + // System.out.println(outputByte); - long counter_ptr = counterNew(new HelloWorld()); + // HelloWorld.factAndCallMeBack(6, new HelloWorld()); - for (int i = 0; i < 5; i++) { - counterIncrement(counter_ptr); - } + // long counter_ptr = counterNew(new HelloWorld()); - counterDestroy(counter_ptr); + // for (int i = 0; i < 5; i++) { + // counterIncrement(counter_ptr); + // } - System.out.println("Invoking asyncComputation (thread id = " + Thread.currentThread().getId() + ")"); - asyncComputation(new HelloWorld()); + // counterDestroy(counter_ptr); + + // System.out.println("Invoking asyncComputation (thread id = " + Thread.currentThread().getId() + ")"); + // asyncComputation(new HelloWorld()); + + + HelloWorld hw = new HelloWorld(); + // hw.setNameStr("fsdfs"); + System.out.println(HelloWorld.fetchNameStr(hw)); + } + + private String nameStr; + + public void setNameStr(String nameStr){ + System.out.println(nameStr); + + // this.nameStr=nameStr; + } + + public String getNameStr(){ + return nameStr; } public void factCallback(int res) { diff --git a/mylib/src/lib.rs b/mylib/src/lib.rs index 7b4c527..6fdec02 100644 --- a/mylib/src/lib.rs +++ b/mylib/src/lib.rs @@ -1,11 +1,27 @@ -use jni::JNIEnv; - -use jni::objects::{GlobalRef, JClass, JObject, JString}; - +use jni::objects::{GlobalRef, JClass, JObject, JString, JValue}; use jni::sys::{jbyteArray, jint, jlong, jstring}; - +use jni::JNIEnv; use std::{sync::mpsc, thread, time::Duration}; +#[no_mangle] +pub extern "system" fn Java_me_ehlxr_HelloWorld_fetchNameStr( + env: JNIEnv, + _class: JClass, + input: JObject, +) -> jstring { + if let JValue::Object(rt) = env + .call_method(input, "getNameStr", "()Ljava/lang/String;", &[]) + .unwrap() + { + println!("return {:?}", rt) + }; + + let output = env + .new_string(format!("Hello, {:?}! from Rust..", input)) + .expect("Couldn't create java string!"); + output.into_inner() +} + #[no_mangle] pub extern "system" fn Java_me_ehlxr_HelloWorld_hello( env: JNIEnv,