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。^_^