jni_rs/java_src/me/ehlxr/HelloWorld.java

87 lines
2.5 KiB
Java
Raw Normal View History

2022-02-22 12:29:33 +00:00
package me.ehlxr;
2022-02-25 01:57:08 +00:00
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
2022-02-22 12:29:33 +00:00
2022-02-23 01:39:30 +00:00
class HelloWorld {
2022-02-24 03:03:22 +00:00
static {
2022-02-24 03:18:34 +00:00
// Linux: export LD_LIBRARY_PATH=/Users/ehlxr/Workspaces/Rust/jni_rs/mylib/target/debug
// Mac: export JAVA_LIBRARY_PATH=/Users/ehlxr/Workspaces/Rust/jni_rs/mylib/target/debug
2022-02-24 03:03:22 +00:00
System.loadLibrary("mylib");
}
2022-02-25 01:57:08 +00:00
public Long no;
2022-02-24 03:03:22 +00:00
private String name;
public int age;
2022-02-25 01:57:08 +00:00
public List<String> ls;
public Map<String, Long> map;
2022-02-24 03:03:22 +00:00
2022-02-23 01:39:30 +00:00
private static native String hello(String input);
2022-02-24 03:03:22 +00:00
2022-02-23 01:39:30 +00:00
private static native byte[] helloByte(byte[] input);
2022-02-24 03:03:22 +00:00
2022-02-23 01:39:30 +00:00
private static native void factAndCallMeBack(int n, HelloWorld callback);
2022-02-24 03:03:22 +00:00
2022-02-23 01:39:30 +00:00
private static native long counterNew(HelloWorld callback);
2022-02-24 03:03:22 +00:00
2022-02-23 01:39:30 +00:00
private static native void counterIncrement(long counter_ptr);
2022-02-24 03:03:22 +00:00
2022-02-23 01:39:30 +00:00
private static native void counterDestroy(long counter_ptr);
2022-02-24 03:03:22 +00:00
private static native void asyncComputation(HelloWorld callback);
2022-02-23 12:37:36 +00:00
2022-02-24 03:03:22 +00:00
private static native String getFiled(HelloWorld param);
2022-02-22 12:29:33 +00:00
public static void main(String[] args) {
2022-02-24 03:03:22 +00:00
String output = HelloWorld.hello("Java");
System.out.println(output);
2022-02-23 12:37:36 +00:00
// byte[] outputByte = HelloWorld.helloByte("byte".getBytes());
// System.out.println(outputByte);
// HelloWorld.factAndCallMeBack(6, new HelloWorld());
// long counter_ptr = counterNew(new HelloWorld());
2022-02-23 01:39:30 +00:00
2022-02-23 12:37:36 +00:00
// for (int i = 0; i < 5; i++) {
2022-02-24 03:03:22 +00:00
// counterIncrement(counter_ptr);
2022-02-23 12:37:36 +00:00
// }
2022-02-23 01:39:30 +00:00
2022-02-23 12:37:36 +00:00
// counterDestroy(counter_ptr);
2022-02-23 01:39:30 +00:00
2022-02-23 12:37:36 +00:00
// System.out.println("Invoking asyncComputation (thread id = " + Thread.currentThread().getId() + ")");
// asyncComputation(new HelloWorld());
2022-02-23 01:39:30 +00:00
2022-02-25 01:57:08 +00:00
List<String> ls = new ArrayList<>();
ls.add("ls1");
ls.add("ls2");
ls.add("ls3");
2022-02-24 03:03:22 +00:00
HelloWorld hw = new HelloWorld();
hw.setName("Jack");
hw.no = 123434555L;
hw.age = 30;
2022-02-25 01:57:08 +00:00
hw.ls = ls;
2022-02-24 03:03:22 +00:00
System.out.println(HelloWorld.getFiled(hw));
2022-02-23 12:37:36 +00:00
}
2022-02-24 03:03:22 +00:00
public String getName() {
return name;
2022-02-23 12:37:36 +00:00
}
2022-02-24 03:03:22 +00:00
public void setName(String name) {
this.name = name;
2022-02-23 01:39:30 +00:00
}
public void factCallback(int res) {
2022-02-24 03:03:22 +00:00
System.out.println("factCallback: res = " + res);
2022-02-23 01:39:30 +00:00
}
public void counterCallback(int count) {
2022-02-24 03:03:22 +00:00
System.out.println("counterCallback: count = " + count);
2022-02-23 01:39:30 +00:00
}
public void asyncCallback(int progress) {
System.out.println("asyncCallback: thread id = " + Thread.currentThread().getId() + ", progress = " + progress + "%");
2022-02-22 12:29:33 +00:00
}
2022-02-23 01:39:30 +00:00
}