<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 閱讀(753) 評論(0)  編輯  收藏 所屬分類: javaZZ
    主站蜘蛛池模板: 99久久免费精品视频| h视频在线观看免费| 又粗又大又黑又长的免费视频 | 国产免费久久精品丫丫| 国产免费人人看大香伊| 美女视频黄a视频全免费网站色| 日韩在线看片免费人成视频播放| 亚洲乱妇老熟女爽到高潮的片| 女人18一级毛片免费观看| 亚洲美国产亚洲AV| 国产大片91精品免费观看男同| 黄网站色成年片大免费高清| 毛茸茸bbw亚洲人| 无码人妻AV免费一区二区三区| 亚洲黄色在线观看视频| 永久免费av无码网站韩国毛片| 亚洲高清国产拍精品熟女| 亚洲AV日韩精品一区二区三区 | 久久久久一级精品亚洲国产成人综合AV区 | 亚洲av色香蕉一区二区三区| 免费国产一级特黄久久| 一区二区三区在线观看免费| 亚洲成Av人片乱码色午夜| 亚洲免费在线视频播放| 亚洲av无码专区国产不乱码| 亚洲日本中文字幕天堂网| 成人性做爰aaa片免费看| 亚洲白色白色永久观看| 精品少妇人妻AV免费久久洗澡| 免费又黄又爽又猛大片午夜| 亚洲AV无码乱码国产麻豆| 免费精品国产自产拍在线观看图片 | 18女人毛片水真多免费| 亚洲欧美精品午睡沙发| 久99精品视频在线观看婷亚洲片国产一区一级在线| 精选影视免费在线 | 亚洲色偷偷av男人的天堂| 国产成人免费片在线观看| 久久午夜无码免费| 亚洲av无码有乱码在线观看| 亚洲精品无码mv在线观看网站|