研究了下java native方法的例子。網上看了下資料。
首先看下什麼是Native method,參考了下http://www.80×86.cn/article.asp?id=1448上的文章
Simply put, a native method is the Java interface to non-Java code. It is Java’s link to the “outside world.” More specifically, a native method is a Java method whose implementation is provided by non-Java code, most likely C。大概意思就是說native方法是通過java調用其他非java的代碼。
in your Java class, you mark the methods you wish to implement outside of Java with the native method modifier-much like you would use the public or static modifiers. Then, rather than supplying the method’s body, you simply place a semicolon in its place.The only thing special about this declaration is that the keyword native is used as a modifier. Every other Java method modifier can be used along with native, except abstract.說的是native關鍵字跟其他什麼public static修飾符一樣也可以用來修飾方法。但除了abstract。This is logical, because the native modifier implies that an implementation exists, and the abstract modifier insists that there is no implementation. Your native methods can be static methods, thus not requiring the creation of an object (or instance of a class).這是合理的,因為native暗示這些方法是有實現體的,只不過這些實現體是非java的,但是abstract卻顯然的指明這些方法無實現體。呵呵。我就不翻譯原文了。有興趣的看看原文去吧。
再查了些資料,就寫了如下的代碼。這個代碼又是helloworld。唉,繼續helloworld吧。用java去調用一個用C實現的方法。
Code:
- /**
- * project_name:nativeTest
- * Description:
- * Copyright: Copyright (c) 2010 by tl3shi.
- **/
- package com.i3zhai.www;
- /**
- * Title: NativeHelloWorld.java
- * Description:測試java關鍵字中native的方法
- * @author : <a href="mailto:tanglei3shi@163.com">tl3shi</a>
- * @date:2010-12-21 下午10:23:24
- * @version 1.0
- */
- public class NativeHelloWorld
- {
- /**
- * tl3shi
- * 下午10:27:23
- * Discription: 構造方法,載入dll檔案
- * 或者用static 引入dll檔案也可以,總之就是要在
- * 程式用到的時候之前把dll載入進來
- */
- public NativeHelloWorld()
- {
- System.loadLibrary("hello");
- }
- /**
- * tl3shi
- * 下午10:28:24
- * Discription:聲明一個native的方法
- * 因為是native的 只是聲明,實現由另外的諸如C語言去實現
- */
- public native void nativeHello();
- /**
- * tl3shi
- * 下午10:28:59
- * Discription:在此類中去調用C語言中實現的方法
- */
- public void sayHello()
- {
- nativeHello();//調用方法
- }
- }
上面已經寫好了注釋了。剛開始的時候,我直接用了eclipse,但發現後來dll檔案容易找不到(eclipse估計估計還得哪裡配置下吧)。於是就改為控制台下。
首先用javac命令 編譯出class檔案。沒有編譯錯誤就會產生class檔案。
然後執行javah命令。看javah 的用法。直接控制台下就能看到
R:/native>javah
用法:javah [選項] <類>
其中 [選項] 包括:
-help 輸出此協助訊息並退出
-classpath <路徑> 用於裝入類的路徑
-bootclasspath <路徑> 用於裝入引導類的路徑
-d <目錄> 輸出目錄
-o <檔案> 輸出檔案(只能使用 -d 或 -o 中的一個)
-jni 產生 JNI樣式的標頭檔(預設)
-version 輸出版本資訊
-verbose 啟用詳細輸出
-force 始終寫入輸出檔案
使用全限定名稱指定 <類>(例如,java.lang.Object)。
直接用javah NativeHelloWorld 就會產生一個.h的檔案。如下所示:
Code:
- /* DO NOT EDIT THIS FILE - it is machine generated */
- #include
- /* Header for class NativeHelloWorld */
-
- #ifndef _Included_NativeHelloWorld
- #define _Included_NativeHelloWorld
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*
- * Class: NativeHelloWorld
- * Method: nativeHello
- * Signature: ()V
- */
- JNIEXPORT void JNICALL Java_NativeHelloWorld_nativeHello
- (JNIEnv * xx, jobject xxx);
-
- #ifdef __cplusplus
- }
- #endif
- #endif
然後在寫hello。c檔案如下所示:
Code:
- #include "NativeHelloWorld.h"
- JNIEXPORT void JNICALL Java_NativeHelloWorld_nativeHello
- (JNIEnv * xx, jobject xxx)
- {
- printf("helloWorld,I am printing in the C ");
- }
JNIEXPORT void JNICALL Java_NativeHelloWorld_nativeHello直接copy標頭檔裡面的就是了,
接下來要做的就是要產生dll檔案。這個dll檔案就是在類NativeHelloWorld中要load的參數名字。產生dll檔案可以用VC6.0之類的工具。期間在build的時候可能會遇到問題。就是上面的方法簽名的地方需要加上參數。如上面的XX,xxx。這個是後來加上去的,不是javah之後就有的.
在build的時候,跟著錯誤資訊就知道了,還會用到其他的dll檔案。這個檔案到jdk安裝目錄下search下就OK了。有jni.h,其中jni.h又會用到jni_md.h。直接copy過來到VC的工作路徑下。然後就能在debug目錄裡面找到dll檔案了。
此時,已經差不多了。再寫個Test就OK了。
Code:
- /**
- * Title: NativeHelloWorld.java
- * Description:測試java關鍵字中native的方法
- * @author : <a href="mailto:tanglei3shi@163.com">tl3shi</a>
- * @date:2010-12-21 下午10:23:24
- * @version 1.0
- */
- public class Test
- {
- /**
- * tl3shi
- * 下午10:23:24
- * Discription:
- *@param args
- */
- public static void main(String[] args)
- {
- NativeHelloWorld nat = new NativeHelloWorld();
- nat.sayHello();
- }
- }
再在控制台下執行javac Test.java java Test就能看到結果了。