Java的JNI快速入門教程(推薦)_java

來源:互聯網
上載者:User

1. JNI簡介

JNI是Java Native Interface的英文縮寫,意為Java本地介面。

問題來源:由於Java編寫底層的應用較難實現,在一些即時性要求非常高的部分Java較難勝任(即時性要求高的地方目前還未涉及,即時性這類話題有待考究)。

解決辦法:Java使用JNI可以調用現有的本地庫(C/C++開發任何和系統相關的程式和類庫),極大地靈活Java的開發。

2. JNI快速學習教程

2.1 問題:

使用JNI寫一段代碼,實現string_Java_Test_helloworld(JNIEnv *env, jclass cls , jstring j_str)函數,實現在字串j_str("world")前面加上hello,並返回。

2.2 解決問題過程:

I. 編寫Test.java類:

public class Test{  // native interface  public native String helloworld(String text);    public static void main(String[] args){    // Load dynamic library    System.loadLibrary("Test2");    Test ts = new Test();    String text = ts.helloworld("world");    System.out.println(text);  }}

備忘:

1、載入動態類庫:System.loadLibrary("Test2");【Windows下載入的就是Test2.dll,Linux下載入的是Test2.so】

II. 編譯Test.java檔案

進入cmd輸入命令 > javac Test.java

III. 產生Test.h檔案

進入cmd輸入命令 > javah Test

Test.h檔案內容如下:

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class Test */#ifndef _Included_Test#define _Included_Test#ifdef __cplusplusextern "C" {#endif/* * Class:   Test * Method:  helloworld * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_Test_helloworld (JNIEnv *, jobject, jstring);#ifdef __cplusplus}#endif#endif

備忘:

1、函式宣告,固定格式:JNIEXPORT;傳回型別:jstring;JNI調用:JNICALL;Java_完整類名_方法名:Java_Test_helloworld;

2、函數參數:調用jni.h封裝好的函數指標:JNIEnv;Java類本身:jobject,Java檔案傳入參數:jstring。

IV. 編寫C語言檔案Test2.c實現Test類調用動態連結程式庫的功能:

#include "Test.h"#include <string.h>JNIEXPORT jstring JNICALL Java_Test_helloworld (JNIEnv *env, jobject obj, jstring string){  const char* str = (*env)->GetStringUTFChars(env,string,0);  char cap[128];  cap[0] = 'h';  cap[1] = 'e';  cap[2] = 'l';  cap[3] = 'l';  cap[4] = 'o';     strcat(cap,str);     (*env)->ReleaseStringUTFChars(env,string,0);  return (*env)->NewStringUTF(env,cap); }

備忘:

1、由於Java本身使用了雙位元組字元,C語言本身都是單位元組字元,所以需要使用(*env)->GetStringUTFChars()轉換Java和C之間的字串;

2、GetStringUTFChars()和NewStringUTF(),第一個是從UTF8轉換為C的編碼格式,第二個是根據C的字串返回一個UTF8字串;

3、ReleaseStringUTFChars()是用來釋放對象的,在Java中有虛擬機器進行記憶體回收,但是在C語言中這些對象必須手動回收,否則可能造成記憶體流失。

V. 編譯和運行

編譯:

進入cmd輸入命令 > gcc -I "D:\Program Files\Java\jdk1.8.0_45\include" -I "D:\Program Files\Java\jdk1.8.0_45\include\win32" --share Test2.c -o Test2.dll

運行:

進入cmd輸入命令 > java Test

運行結果如下:

helloworld

3. 總結:

第一步:編寫帶有native方法的Java類(Test.java),使用javac工具編譯Java類(產生Test.class);

第二步:使用javah產生與native方法對應的標頭檔(Test.h);

第三步:使用C/C++實現相應的標頭檔(Test2.c),並編譯為動態連結程式庫(Test2.so)。

本文運行環境:Windows 64位作業系統,JDK 1.8版本,mingw64(GCC)。

以上這篇Java的JNI快速入門教程(推薦)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.