2010年9月28日 星期二

Java Native Interface (JNI) - Introduction

Why JNI?
  • Native applications are written in native programming languages such as C and C++, compiled into host-specific binary code, and linked with native libraries.
    • Native applications and native libraries are typically dependent on a particular host environment.
    • A C application built for one operating system, for example, typically does not work on other operating systems.
  • Java platforms are commonly deployed on top of a host environment.
    • The Java platform offers a set of features that applications can rely on independent of the underlying host environment.
    JNI allows Java code running in a Java Virtual Machine (JVM) to call and to be called by native applications and libraries written in other languages, such as C, C++.
    主要基於以下三個需求因應而生:
  1. 解決性能問題
    • 採用JNI技術針對一些嚴重影響Java性能的部分程式碼,該部分可能只占原始程式的極少部分,所以幾乎可以不考慮該部分代碼在主流平臺之間移植的工作量。同時,也不必過分擔心類型匹配問題,我們完全可以控制程式碼不出現這種錯誤。此外,也不必擔心安全控制問題,因爲Java安全模型已擴展爲允許非系統類別載入和呼叫本地方法。根據Java規範,從JDK 1. 2開始,FindClass將設法找到與當前的本地方法關聯的類別載入器。如果平臺相關程式碼屬於一個系統類別,則無需涉及任何類別載入器;否則,將呼叫適當的類別載入器來載入和鏈結已命名的類別。換句話說,如果在Java程式中直接呼叫C/C++語言産生的機器碼,該部分代碼的安全性就由Java虛擬機器控制。
  2. 解決本機平臺介面呼叫問題
    • 由於Java跨平臺的特性,使得它與本機的各種內部聯繫變得很少,約束了它的功能。通過JNI呼叫native methods,使Java可以實現和本地機器的緊密聯繫,呼叫系統級的各介面方法。而native methods是以shared library文件的形式存放的(在WINDOWS平臺上是DLL文件形式,在UNIX機器上是SO文件形式)
  3. 嵌入式開發應用
    • Write once, run anywhere (WORA)
    • Java應用程式能夠在帶有JVM的任何硬軟體系統上執行。加上Java語言本身所具有的安全性、可靠性和可攜性等特點,對實現瘦身上網的資訊家電等網路設備十分有利。也正是由於JNI解決了本機平臺介面呼叫問題,於是JNI在嵌入式開發領域也是炙手可熱。

Role of JNI!
    A two-way interface that allows Java applications to invoke native code and vice versa.
  • Users use the JNI to write native methods that allow Java applications to call functions implemented in native libraries. Java applications call native methods in the same way that they call methods implemented in the Java programming language.
  • The JNI supports an invocation interface that allows users to embed a Java virtual machine implementation into native applications. Native applications can link with a native library that implements the Java virtual machine, and then use the invocation interface to execute software components written in the Java programming language.


When to use JNI?
  1. To access system features not otherwise available in the Java language or an API.
  2. To hook to existing libraries of usable native code.
  3. To improve the performance of time-critical methods.

Implications of Using the JNI
  1. Java applications that depend on the JNI can no longer readily run on multiple host environments.
    • Necessary to recompile the part of the application written in native programming languages.
    • Write once, run anywhere” would be lost.
  2. Java programming language is type-safe and secure, native languages such as C or C++ are not.
    • A misbehaving native method can corrupt the entire application. Java applications are subject to security checks before invoking JNI features.

Referenced from "The Java Native Interface - Programmer's Guide and Specification" Chapter 1

0 意見: