Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lanhuidong/jni-demo
JNI示例代码
https://github.com/lanhuidong/jni-demo
Last synced: 6 days ago
JSON representation
JNI示例代码
- Host: GitHub
- URL: https://github.com/lanhuidong/jni-demo
- Owner: lanhuidong
- License: apache-2.0
- Created: 2017-05-02T14:58:28.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-01-18T02:41:17.000Z (almost 5 years ago)
- Last Synced: 2024-11-09T07:32:47.991Z (2 months ago)
- Language: C
- Size: 10.7 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jni-demo
1. 编写Java类, 定义native方法, 如: com.nexusy.jni.JniService.java2. 创建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)