posts - 241,  comments - 116,  trackbacks - 0

          第一步:編寫Java端代碼

          定義一個Java類

          public class TestNativeDemo {
           
              // 聲明本地方法
              public native String testJni(String arg);
           
              static {
                  // 加載DLL文件
                  System.loadLibrary("TestNativeDemoCPP");
              }
           
              public static void main(String args[]) {
                  TestNativeDemo ob = new TestNativeDemo();
                  // 調用本地方法
                  String result = ob.testJni("Hello,Jni"); // call a native method
                  System.out.println("TestNativeDemo.testJni=" + result);
              }
          }
          ----------------------------------------------

          編譯之后在生成TestNativeDemo.class的bin目錄下執行javah TestNativeDemo命令生成頭文件TestNativeDemo.h

          ----------------------------------------------

          /* DO NOT EDIT THIS FILE - it is machine generated */sky7034
          #include <jni.h>
          /* Header for class TestNativeDemo */
           
           
          #ifndef _Included_TestNativeDemo //避免重復包含頭文件  
          #define _Included_TestNativeDemo
          #ifdef __cplusplus //c++編譯環境中才會定義__cplusplus  
          extern "C" { //告訴編譯器下面的函數是c語言函數(因為c++和c語言對函數的編譯轉換不一樣,主要是c++中存在重載)   
           
          #endif
          /*
           * Class:     TestNativeDemo
           * Method:    testJni
           * Signature: (Ljava/lang/String;)Ljava/lang/String;
           */
          JNIEXPORT jstring JNICALL Java_TestNativeDemo_testJni
            (JNIEnv *, jobject, jstring);
           
          #ifdef __cplusplus
          }
          #endif
          #endif
          posted on 2012-01-12 09:43 墻頭草 閱讀(737) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 六枝特区| 穆棱市| 洪洞县| 海伦市| 葵青区| 静乐县| 宝山区| 泽普县| 蒲江县| 保德县| 吉水县| 梁平县| 萨嘎县| 淮滨县| 綦江县| 梓潼县| 遵义县| 遵化市| 中西区| 巫山县| 习水县| 宁远县| 镇赉县| 陵川县| 永仁县| 西乡县| 柳州市| 濮阳县| 那坡县| 阆中市| 洪江市| 广宗县| 保康县| 健康| 资中县| 白山市| 齐河县| 霍州市| 乐业县| 昆明市| 贺州市|