Java本地介面
JNI(Java Native Interface,Java 本地介面)是一種編程框殼,予得 Java 虛擬機器當中的 Java 程式會當呼叫本地應用 / 抑庫仔,嘛會當予其他的程式呼叫。本地程式一般是用其他的語言(C、C + + 抑是組合語言等等)編寫的,並且予人編譯做是因為本機硬體佮作業系統的程式。
設計的佮功能
有的代誌 Java 無法度處理的時陣,JNI 允准程式設計師用其他程式語言來解決,比如講,Java 標準庫不支援的平台相關功能或者是程式庫。嘛用佇咧改造已經存在的用其他的語言來寫的程式,供 Java 程式呼叫。真濟無仝款 JNI 的標準庫提供了真濟功能予程式的設計師使用,譬如講檔案 I / O、音訊相關的功能。當然喔,嘛有各種高效能的程式,閣有𪜶的平台有關的 API 實現,允准所有 Java 應用程式安全而且平台獨立地使用這寡功能。
JNI 框殼允准 Native 方法呼叫 Java 東西,就親像 Java 程式存取 Native 物件仝款方便。Native 方法會當建立 Java 東西,讀遮的物件,並呼叫 Java 物件執行某一寡方法。當然喔 Native 步數嘛會當讀由著 Java 程式家己建立的物件,並且呼叫遮的物件的方法。
注意事項
- 咧使用 JNI 的過程中,可能因為某一寡微細仔 BUG,著規个 JVM 造成真歹重現佮除錯的錯誤。
- 只有應用程式和簽章的 applet 會當呼叫 JNI。
- 依賴佇咧 JNI 的應用失去了 Java 的平台移植性(一種解決辦法是為逐个平台咧編寫專門的 JNI 代碼,然後佇 Java 代碼中,根據作業系統載入正確的 JNI 代碼)。
- JNI 框架並無著 non-JVM 記憶體提供自動糞埽回收機制,Native 代碼(如組合語言)分配的記持體佮資源,需要其家己負責進行顯式的釋放。
- Linux 佮 Solaris 平台,若是 Native 代碼將自身註冊做訊號處理器(signal handler), 就會閘落來予 JVM 的訊號。會當使用責任鏈模式予 Native 代碼閣較好的佮 JVM 咧進行互動。
- Windows 平台頂,佇咧 SEH try / catch 塊中會當共結構化例外處理(SEH)用來包裝 Native 代碼,以揣機器(CPU / FPU)成做的軟中斷(比如講:空指標異常、被除數為零等), 將遮的中斷佇傳播到 JVM(中的 Java 代碼)進前進行處理,以免造成未揣出的異常。
- NewStringUTF、GetStringUTFLength、GetStringUTFChars、ReleaseStringUTFChars 佮 GetStringUTFRegion 等編碼函式處理的是一種修改的 UTF ma八,, 實際上是一種無仝的編碼,某一寡字元並毋是標準的 UTF ma八。null 字元(U + 零)以及無佇咧 Unicode 字元平面對映中的字元(codepoints 大於等於 U + 一鈕的字元,比如講 UTF 鋪十六中的代理著 surrogate pairs), 咧修改的 UTF 鋪八中的編碼攏有無仝。真濟程式錯誤地使用矣遮的函式,將標準 UTF 鋪八字傳入去抑是傳出遮的函式,實際上應該使用修改了後的編碼。程式應當先使用 NewString、GetStringLength、GetStringChars、ReleaseStringChars、GetStringRegion、GetStringCritical 佮 ReleaseStringCritical 等函式,遮的函式佇尾序機器咧使用 UTF 鋪十六 LE 編碼,佇大尾序機器頂頭使用 UTF 鋪十六 BE 編碼,才閣通過程式共 UTF 鋪十六轉換做 UTF ma八。
- JNI 佇某一寡情形下可能帶來真大的開銷佮效能損失:
- 搧叫 JNI 辦法是真戇重的操作,特別是佇咧多次的重複呼叫的情況下。
- Native 方法袂去予人 JVM 行內,嘛袂去予人即時編譯最佳化,因為方法已經予人編譯過矣。
- Java 陣列可能會予人審查一份,才傳予 native 方法,執行了後才閣審貝轉去。其開銷佮陣列的長度是線性相關的。
- 若是傳送一个物件予方法,抑是需要一个回呼,遐爾 Native 方法有可能會家己呼叫 JVM。存取 Java 東西的屬性、撇步佮類型的時陣,Native 代碼需要類似反射的物件。簽章由字串指定,通常對 JVM 中查詢。這非常的慢慢仔並且容易出錯。
- Java 中的字捾(String)嘛是東西,有 length 屬性,並且是編碼過的。讀抑是建立字串攏需要一擺時間複雜度做 O ( n ) 的複製操作 .
JNI 按怎做工課
佇咧 JNI 框架,native 方法一般佇單獨的 . c 抑是 . cpp 檔案中實現。當 JVM 呼叫遮的函式,就傳達一个 ` JNIEnv ` 指標,一个 ` jobject ` 的指標,任何在 Java 方法內底聲明的 Java 參數。一个 JNI 函式看起來類似按呢:
_ ` env ` _ 指向一个結構包含矣到 JVM 的介面,包含著所有必須的函式佮 JVM 互動、存取 Java 東西。比如講,共本地陣列轉換做 Java 陣列的 JNI 函式,共這个本地的字提轉做 Java 字捾的 JNI 函式,實例化物件,抌異常等。基本上,Java 程式會當做的任何代誌攏會當用 ` JNIEnv ` 做甲,雖然真無簡單。
比如講,下跤代碼共 Java 字串轉化作本地字串:
本地資料的類型佮 Java 資料類型會當互相對映。對複合資料的類型,如東西,陣列,字捾,著愛用 ` JNIEnv ` 中的方法來顯示轉換。
第二个參數 _ obj _ 參照著一个 Java 東西,佇其中聲明矣本地方法。
類型對映
下表是 Java ( JNI ) 佮本地代碼之間的資料類型對映:
簽章 ` " L fully-qualified-class ; " ` 是由該名指明的類。比如講,簽章 ` " Ljava / lang / String ; " ` 是類 ` java . lang . String `。帶字首 ` [ ` 的簽章表示該類型的陣列,如 ` [ I ` 表示規型陣列。` void ` 簽章使用 ` V ` 代碼。
這寡類型是會當相換的,如 ` jint ` 嘛會使用 ` int `,無需要任何類型轉換。
猶毋過,Java 字捾、陣列佮本地字捾、陣列是無仝的。若咧使用 ` char * ` 代替矣 ` jstring `,程式可能會致使 JVM 幫敗。
這款的狀況嘛適合 Java 陣列。落例對陣列元素求和。
JNIEnv \ *
JNI 環境指標 ( JNIEnv \ * ) 作為每一个對映為 Java 方法的本地函式的第一个參數,予本地函式會當佮 JNI 環境互動。這乎 JNI 介面指標會當儲存,但是干焦佇當前執行緒中有效。其他執行緒著愛代先先呼叫 AttachCurrentThread ( ) 共家己附加到虛擬的機器以獲得 JNI 介面指標。一旦附加,本地執行緒執行就類似執行本地函式的正常 Java 執行緒。本地執行緒直甲執行 DetachCurrentThread ( ) 共家己脫離虛擬的機器。
共當做執行緒附加到虛擬機器並取得 JNI 介面指標:
` ` ` JNIEnv * env ; ( * g _ vm )-> AttachCurrentThread ( g _ vm , ( void * * ) & env , NULL ) ; ` ` `
彼當陣執行緒脫離虛擬機器:
` ` ` ( * g _ vm )-> DetachCurrentThread ( g _ vm ) ; ` ` `
進階使用
在地 AWT 畫製
本地代碼毋但會當佮 Java 互動,嘛會當閣 Java ` Canvas ` 繪圖,使用 Java AWT Native Interface。
存取組譯代碼
JNI 允准直接存取組譯代碼。嘛會當對組譯代碼的存取 Java。
Microsoft 的 RNI
Microsoft 實現的 Java 虛擬機器—— Visual J + + 的類似的存取本地 Windows 代碼的機制Raw Native Interface(RNI)。
例
HelloWorld
make . sh
build . bat
HelloWorld . java
HelloWorld . h
libHelloWorld . c
Invocation :
參見
- Java AWT Native Interface
- Gluegen , a Java tool which automatically generates the Java and JNI code necessary to call C libraries from Java code
- P / Invoke , the . NET Framework method of calling native applications
- SWIG , a multilanguage interface-generator for C and C + + libraries that can generate JNI code
- Java Native Access provides Java programs easy access to native shared libraries without writing boilerplate code
參考文獻
相關冊
外部連結
- Oracle's JNI page for Java 六 , including the JNI Specification
- Java Native Interface : Programmer's Guide and Specification-Book , copyright 兩千空二 .
- Best practices for using the Java Native Interface
- JNI Complete tutorial with examples
- GNU CNI Tutorial
- Multi-platform JNI Tutorial at Think-Techie . com
- A JNI Tutorial at CodeProject . com ( Microsoft specific )
- JNI Tutorial at CodeToad . com
- Larger JNI example from Sun
- JNI video tutorial with Eclipse and Visual Studio
- JNI in XCode from Apple
- Exception handling in JNI
- HawtJNI Simplifies creating JNI libraries by code generating the JNI implementations using declarative annotations placed on your Java code .
- J / Link lets you call Java from Mathematica in a completely transparent way , and it also lets you use and control the Mathematica kernel from a Java program ( Commercial )
- Jace is a toolkit designed to make it easy to write JNI-based programs
- JNIWrapper provides simplified access to native code from Java applications without using Java Native Interface .
- Java to Native Interface LGPL library to call native functions from Java
- 永久失效連結 ] Java Native Access Access to native libraries from Java without JNI
- NLink Another library for access to native libraries without JNI
- NativeCall–call native methods from Java without JNI Library to access native code without JNI
- JNIEasy Transparent Native Programming for C / C + + , pure Java alternative to JNI using POJOS and JDO / JPA development style
- jni 四 net bridge between Java and . NET ( intraprocess , fast , object oriented , open-source )
- Object-Oriented JNI Advanced Add-in for VC 六 Object-Oriented JNI with a number of helpers that includes the standard JNI SDK for regular C + + ( Commercial )
- Object-Oriented JNI for . NET 一孵一 ( low-level ) Object-Oriented JNI with a number of helpers that includes the standard JNI SDK for C # , Managed C + + , VB # , J # ( Commercial )
- Object-Oriented JNI for . NET 二孵空 ( low-level ) Object-Oriented JNI with a number of helpers that includes the standard JNI SDK for C # , Managed C + + , VB # , J # ( Commercial )
- OOJNI Add-in ( C # , VB # ) for VS 兩千空八分之二千空五 Generates object-oriented JNI code in C # or VB # for Java classes selected , implements Java interfaces and Java native methods in VB # and C # . Java Class methods and fields ( which are represented as . NET Class properties ) to be wrapped can be filtered . ( Commercial )
- _ e _ X _ treme _ DB JNI uses Java annotations and reflection to enable Java applications to call the EXtremeDB database ( written in C ) without reliance on an external database definition language
- JavaToDPR , a Delphi Project ( . dpr ) Stub File Generator that allows one to write an Embarcadero Delphi DLL to handle the native methods declared in a Java . class file