JNI——实现Java与C的协同工作(五)
2014-01-15 02:11
232 查看
第五部分 JNI的进化——JNA
JNA 的前世今生
1. Wikipedia(en) 的介绍
Java Native Access provides Java programs easy access to nativesharedlibraries without using the Java Native Interface. JNA's design aims to provide native access in a natural way with a minimum of effort.
No boilerplate or generated glue code is required.
PS:鉴于官方的说明比较啰嗦,就没有引用。
2.个人理解
简单来说,JNA 已经帮我们搞定了常用 C 中函数(甚至还有其他语言)的 JNI 链接,并封装成JAR形式的包供我们直接调用。可以说,这对于一般应用来讲是非常方便的,当然,对于那些需要我们自定义的底层函数还是要老老实实地走 JNI 的流程。JNA 的使用
1. 获取 JNA
官方网站(GitHub):https://github.com/twall/jna2. 将 JNA 的 JAR 包导入工程
3. 编写程序 HelloWorld.java (来自 JNA 的示例)
import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Platform; /** Simple example of JNA interface mapping and usage. */ public class HelloWorld { // This is the standard, stable way of mapping, which supports extensive // customization and mapping of Java to native types. public interface CLibrary extends Library { // 这是JNA已经封装好的方法,可实现C中的printf CLibrary INSTANCE = (CLibrary) Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class); void printf(String format, Object... args); } public static void main(String[] args) { CLibrary.INSTANCE.printf("Welcome to JNA world!\n"); // 相当于C中的printf函数 for (int i=0;i < args.length;i++) { CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]); } } }
4. 编译运行
结果如下:Welcome to JNA world!
更多 JNA 使用说明
参考官方文档:http://twall.github.io/jna/4.0/javadocJNI 与 JNA 性能比较
参考这篇文章:http://blog.csdn.net/drifterj/article/details/7841810相关文章推荐
- JNI——实现Java与C的协同工作(二)
- JNI——实现Java与C的协同工作(三)
- JNI——实现Java与C的协同工作(四)
- JNI——实现Java与C的协同工作(一)
- linux下使用jni实现c++调用java程序(1)准备工作
- Java 网络编程---分布式文件协同编辑器设计与实现
- JAVA应用JNI调用OpenCV实现人脸检测
- java JNI 实现原理 (三) JNI中的RegisterNatives方法
- eclipse JNI示例。利用C/C++实现JAVA中调用的函数
- java实现oracle 快速卸载数据并ftp上传数据给各个其他业务系统[从学习到工作(一)]
- 快速实现ARM和DSP的通信和协同工作(一)
- 动态规划求最大工作价值(java实现)
- 用JNI实现java调用C
- 通过JNI实现Java和C++的相互调用
- Android使用JNI实现Java与C之间传递数据
- Android Studio Jni开发(二)实现Native调用java方法和Native调用Android API
- [Android]-图片JNI(C++\Java)高斯模糊的实现与比较
- Java调用C/C++实现的DLL动态库——JNI
- 工作中常用的XML解析Java代码实现
- Android使用JNI实现Java与C之间传递数据