第一部分 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++等很容易做到的事情。