<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jinfeng_wang

    G-G-S,D-D-U!

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks
    http://www.inonit.com/cygwin/jni/helloWorld/load.html?


    There are two different ways to load a native library into a running Java program: System.loadLibrary(String) and System.load(String). The System.loadLibrary method allows us to load a library from the "default" path. System.load allows us to load a library from anywhere via its absolute path.

    First, System.loadLibrary. We'll use System.loadLibrary for our example because most other examples use it, and because we're doing all of our work in one directory.

    When using System.loadLibrary, the only thing we specify is the name of the DLL file we want. The JVM will search for it in the "Java library path." This is a path which is given by the java.library.path system property (and hence can be altered on the java.exe command line using the -D option). The default value of this appears to be related to the Windows path, though it appears to be somewhat scrambled, and I'm not quite sure how or why. In other words, I'm not sure how the Windows JVM creates the initial value of java.library.path. This is the default on my system:

    java.library.path=C:\WINNT\system32;.;C:\WINNT\System32;C:\WINNT;c:\applications\asc\pervasive\BIN;c:\cygwin\bin;
    C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;\win32
    
    C:\>echo %PATH%
    c:\applications\asc\pervasive\BIN;c:\cygwin\bin;C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;\win32
    Note that the current directory is inserted into the PATH; I believe that this is something that Windows does by default. I am going to execute the program from the directory in which HelloWorld.dll was created, so I won't have to mess with java.library.path. One could also use command-line options to alter java.library.path or simply copy the DLL into one of the Windows directories.

    In order to cause the JVM to load the library, we need to alter our Java code:


    package example.jni;
    
    public class HelloWorld {
    	private static native void writeHelloWorldToStdout();
    	
    	public static void main(String[] args) {
    		System.loadLibrary("HelloWorld");
    		writeHelloWorldToStdout();
    	}
    }

    Go ahead and re-compile the Java program.

    The JVM takes care of resolving "HelloWorld" to "HelloWorld.dll" (on UNIX, it would resolve it to "libHelloWorld.so").

    Anyway, we're finished and can proceed to the next step.

    If we were to use the System.load method, we'd just insert code like:

    System.load("c:/path/to/dll/HelloWorld.dll");
    By the way, that's a good non-JNI tip -- the JVM accepts forward slashes just fine, even for Windows file names. Some of my programs (e.g., web programs which involve forward slashes in URLs) have been vastly simplified knowing that, not to mention the lack of annoying escape characters. (Disclaimer: As far as I know, this behavior isn't part of the Java specification, so it's possible it could be changed or operate differently in other VMs.)


    ===============================
    java服務函數的dll實現,就是你用c或c++寫的dll,這個文件要放在java.library.path,
    而java.library.path里有許多路徑,如c:\jdk142\bin,你可以打印出來看一下:
    (System.out.println(System.getProperty("java.library.path"));
    ===============================

    學習了一下JNI,發表文章的時候不知道該選什么好了,不知道JNI應該屬于那個范疇^_^。
      
      1.簡介
      
      JNI是Java Native Interface的縮寫,它的設計目的是:
      
      The standard Java class library may not support the platform-dependent features needed by your application.
      
      You may already have a library or application written in another programming language and you wish to make it accessible to Java applications
      
      You may want to implement a small portion of time-critical code in a lower-level programming language, such as assembly, and then have your Java application call these functions
      
      2.JNI的書寫步驟
      
      編寫帶有native聲明的方法的java類
      
      使用javac命令編譯所編寫的java類
      
      使用javah ?jni java類名生成擴展名為h的頭文件
      
      使用C/C++實現本地方法
      
      將C/C++編寫的文件生成動態連接庫
      
      ok
      
      1) 編寫java程序:
      
      這里以HelloWorld為例。
      
      代碼1:
      
      class HelloWorld {
      public native void displayHelloWorld();
      
      static {
      System.loadLibrary("hello");
      }
      
      public static void main(String[] args) {
      new HelloWorld().displayHelloWorld();
      }
      }
      
      聲明native方法:如果你想將一個方法做為一個本地方法的話,那么你就必須聲明改方法為native的,并且不能實現。其中方法的參數和返回值在后面講述。
      
      Load動態庫:System.loadLibrary("hello");加載動態庫(我們可以這樣理解:我們的方法displayHelloWorld()沒有實現,但是我們在下面就直接使用了,所以必須在使用之前對它進行初始化)這里一般是以static塊進行加載的。同時需要注意的是System.loadLibrary();的參數“hello”是動態庫的名字。
      
      main()方法
      
      2) 編譯沒有什么好說的了
      
      javac HelloWorld.java
      
      3) 生成擴展名為h的頭文件
      
      javah ?jni HelloWorld
      
      頭文件的內容:
      /* DO NOT EDIT THIS FILE - it is machine generated */
      #include <jni.h>
      /* Header for class HelloWorld */
      
      #ifndef _Included_HelloWorld
      #define _Included_HelloWorld
      #ifdef __cplusplus
      extern "C" {
      #endif
      /*
      * Class:   HelloWorld
      * Method:  displayHelloWorld
      * Signature: ()V
      */
      JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
      (JNIEnv *, jobject);
      
      #ifdef __cplusplus
      }
      #endif
      #endif
      
      (這里我們可以這樣理解:這個h文件相當于我們在java里面的接口,這里聲明了一個Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject);方法,然后在我們的本地方法里面實現這個方法,也就是說我們在編寫C/C++程序的時候所使用的方法名必須和這里的一致)。
      
      4) 編寫本地方法
      
      實現和由javah命令生成的頭文件里面聲明的方法名相同的方法。
      
      代碼2:
      
      1 #include <jni.h>
      2 #include "HelloWorld.h"
      3 #include <stdio.h>
      4 JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
      {
      printf("Hello world!\n");
      return;
      }
      
      注意代碼2中的第1行,需要將jni.h(該文件可以在%JAVA_HOME%/include文件夾下面找到)文件引入,因為在程序中的JNIEnv、jobject等類型都是在該頭文件中定義的;另外在第2行需要將HelloWorld.h頭文件引入(我是這么理解的:相當于我們在編寫java程序的時候,實現一個接口的話需要聲明才可以,這里就是將HelloWorld.h頭文件里面聲明的方法加以實現。當然不一定是這樣)。然后保存為HelloWorldImpl.c就ok了。
      
      5) 生成動態庫
      
      這里以在Windows中為例,需要生成dll文件。在保存HelloWorldImpl.c文件夾下面,使用VC的編譯器cl成。
      
      cl -I%java_home%\include -I%java_home%\include\win32 -LD HelloWorldImp.c -Fehello.dll
      
      注意:生成的dll文件名在選項-Fe后面配置,這里是hello,因為在HelloWorld.java文件中我們loadLibary的時候使用的名字是hello。當然這里修改之后那里也需要修改。另外需要將-I%java_home%\include -I%java_home%\include\win32參數加上,因為在第四步里面編寫本地方法的時候引入了jni.h文件。
      
      6) 運行程序
      
      java HelloWorld就ok。^_^

    posted on 2007-03-23 10:23 jinfeng_wang 閱讀(756) 評論(0)  編輯  收藏 所屬分類: javaZZ
    主站蜘蛛池模板: 亚洲女人被黑人巨大进入| 成人永久福利免费观看| 成人黄页网站免费观看大全 | 国产亚洲人成网站在线观看不卡 | 一区二区三区福利视频免费观看| 亚洲无码黄色网址| 羞羞漫画在线成人漫画阅读免费| 成人a视频片在线观看免费| 好看的亚洲黄色经典| xxxxx做受大片在线观看免费| 91精品免费久久久久久久久| 免费一级毛片在线观看| 亚洲AV成人片无码网站| 成人毛片免费视频| 亚洲高清中文字幕免费| 国产一级淫片a免费播放口| 成在人线AV无码免费| 亚洲天堂2016| 免费看韩国黄a片在线观看| 亚洲精品无码午夜福利中文字幕 | 成人亚洲国产va天堂| 91精品免费国产高清在线| 奇米影视亚洲春色| fc2成年免费共享视频网站| 亚洲乱码中文字幕综合234| 一级毛片成人免费看a| 亚洲国产精品日韩| 亚洲一区二区三区在线| 无码国产精品一区二区免费式影视| 国产亚洲?V无码?V男人的天堂| 九九久久国产精品免费热6| 亚洲午夜精品第一区二区8050| eeuss免费天堂影院| 在线亚洲午夜理论AV大片| 高清永久免费观看| 国产小视频在线免费| 美女羞羞喷液视频免费| 亚洲另类少妇17p| 国产又黄又爽又大的免费视频| 亚洲AV美女一区二区三区| 男男gay做爽爽的视频免费|