第一部分 JNI简介
首先是摘自Java SE Doucumentation的官方描述:
JavaTM Native Interface (JNI) is a standard programming interface for writing Java native methods and embedding the JavaTM virtual machine* into native applications. The primary goal is binary compatibility of native method libraries across all Java virtual machine implementations on a given platform.
然后是Wikipedia (en)的介绍:
The Java Native Interface (JNI) is a programming framework that enables Java code running in aJava Virtual Machine (JVM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly.
接着插入百度百科的一张图来形象理解JNI:
最后是个人的简单理解:
为什么我们要使用JNI?大家都知道Java做开发方便快捷,而无论开发还是维护的成本都较低,但任何事情都是要付出代价的,Java也不是万能的。当你遇到Java很难解决或是根本无法解决的问题时,最朴素的思想就是能不能借助C/C++等语言来支援它,而此时,JNI便应运而生。
因此,我认为JNI主要应用在以下两种情况:
1. Java做不到而用C/C++等能做到的事情;
2. Java实现很麻烦而用C/C++等很容易做到的事情。