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服務(wù)函數(shù)的dll實(shí)現(xiàn),就是你用c或c++寫的dll,這個(gè)文件要放在java.library.path,
而java.library.path里有許多路徑,如c:\jdk142\bin,你可以打印出來(lái)看一下:
(System.out.println(System.getProperty("java.library.path"));
===============================
學(xué)習(xí)了一下JNI,發(fā)表文章的時(shí)候不知道該選什么好了,不知道JNI應(yīng)該屬于那個(gè)范疇^_^。
1.簡(jiǎn)介
JNI是Java Native Interface的縮寫,它的設(shè)計(jì)目的是:
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類名生成擴(kuò)展名為h的頭文件
使用C/C++實(shí)現(xiàn)本地方法
將C/C++編寫的文件生成動(dòng)態(tài)連接庫(kù)
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方法:如果你想將一個(gè)方法做為一個(gè)本地方法的話,那么你就必須聲明改方法為native的,并且不能實(shí)現(xiàn)。其中方法的參數(shù)和返回值在后面講述。
Load動(dòng)態(tài)庫(kù):System.loadLibrary("hello");加載動(dòng)態(tài)庫(kù)(我們可以這樣理解:我們的方法displayHelloWorld()沒(méi)有實(shí)現(xiàn),但是我們?cè)谙旅婢椭苯邮褂昧?,所以必須在使用之前?duì)它進(jìn)行初始化)這里一般是以static塊進(jìn)行加載的。同時(shí)需要注意的是System.loadLibrary();的參數(shù)“hello”是動(dòng)態(tài)庫(kù)的名字。
main()方法
2) 編譯沒(méi)有什么好說(shuō)的了
javac HelloWorld.java
3) 生成擴(kuò)展名為h的頭文件
javah ?jni HelloWorld
頭文件的內(nèi)容:
/* 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
?。ㄟ@里我們可以這樣理解:這個(gè)h文件相當(dāng)于我們?cè)趈ava里面的接口,這里聲明了一個(gè)Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject);方法,然后在我們的本地方法里面實(shí)現(xiàn)這個(gè)方法,也就是說(shuō)我們?cè)诰帉慍/C++程序的時(shí)候所使用的方法名必須和這里的一致)。
4) 編寫本地方法
實(shí)現(xiàn)和由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文件夾下面找到)文件引入,因?yàn)樵诔绦蛑械腏NIEnv、jobject等類型都是在該頭文件中定義的;另外在第2行需要將HelloWorld.h頭文件引入(我是這么理解的:相當(dāng)于我們?cè)诰帉慾ava程序的時(shí)候,實(shí)現(xiàn)一個(gè)接口的話需要聲明才可以,這里就是將HelloWorld.h頭文件里面聲明的方法加以實(shí)現(xiàn)。當(dāng)然不一定是這樣)。然后保存為HelloWorldImpl.c就ok了。
5) 生成動(dòng)態(tài)庫(kù)
這里以在Windows中為例,需要生成dll文件。在保存HelloWorldImpl.c文件夾下面,使用VC的編譯器cl成。
cl -I%java_home%\include -I%java_home%\include\win32 -LD HelloWorldImp.c -Fehello.dll
注意:生成的dll文件名在選項(xiàng)-Fe后面配置,這里是hello,因?yàn)樵贖elloWorld.java文件中我們loadLibary的時(shí)候使用的名字是hello。當(dāng)然這里修改之后那里也需要修改。另外需要將-I%java_home%\include -I%java_home%\include\win32參數(shù)加上,因?yàn)樵诘谒牟嚼锩婢帉懕镜胤椒ǖ臅r(shí)候引入了jni.h文件。
6) 運(yùn)行程序
java HelloWorld就ok。^_^