JNI——實現Java與C的協同工作(一)

第一部分  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:

圖案1


     最後是個人的簡單理解:

        爲什麼我們要使用JNI?大家都知道Java做開發方便快捷,而無論開發還是維護的成本都較低,但任何事情都是要付出代價的,Java也不是萬能的。當你遇到Java很難解決或是根本無法解決的問題時,最樸素的思想就是能不能借助C/C++等語言來支援它,而此時,JNI便應運而生。

        因此,我認爲JNI主要應用在以下兩種情況:

        1. Java做不到而用C/C++等能做到的事情;

        2. Java實現很麻煩而用C/C++等很容易做到的事情。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章