Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lanhuidong/jni-demo

JNI示例代码
https://github.com/lanhuidong/jni-demo

Last synced: 6 days ago
JSON representation

JNI示例代码

Awesome Lists containing this project

README

        

# jni-demo
1. 编写Java类, 定义native方法, 如: com.nexusy.jni.JniService.java

2. 创建C/C++ 头文件, 进入到JniService.java所属package所在目录, 例如:com文件夹所在目录

Java 9及之前版本:

```shell
javah com.nexusy.jni.JniService
```

Java 10及之后版本:

```shell
javac -h . com/nexusy/jni/JniService.java
```

3. 编写C/C++代码

4. 将C/C++代码编译为动态库

macOS:

```shell
clang -Wall -I$JAVA_HOME/include/ -I$JAVA_HOME/include/darwin/ -L. -lxxx -shared -undefined dynamic_lookup com_nexusy_jni_JniService.c -o libdemo.jnilib
```

Linux:

```shell
gcc -Wall -D jni_log -shared -fPIC -I$JAVA_HOME/include -I$JAVA_HOME/include/linux -L. -lxxx com_nexusy_jni_JniService.c -o libdemo.so
```

5. 运行java Demo

```shell
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
java -Djava.library.path=. Demo
```

### 参考资料:

[http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/jniTOC.html](http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/jniTOC.html)

[https://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html](https://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html)