1、编写TestDll.java文件
public class TestDll {
static {
System.loadLibrary("testdll");
}
public native static void set(int i);
public native static int get();
public static void main(String[] args) {
TestDll d = new TestDll();
d.set(10);
System.out.println(d.get());
}
}
2、将该文件编译
/*
* javac -d . TestDll.java
* javah com.mine.dll.jni.TestDll
*/
3、javah com.mine.dll.jni.TestDll 生成TestDll.h文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_mine_dll_jni_TestDll */
#ifndef _Included_com_mine_dll_jni_TestDll
#define _Included_com_mine_dll_jni_TestDll
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_mine_dll_jni_TestDll
* Method: set
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_mine_dll_jni_TestDll_set
(JNIEnv *, jclass, jint);
/*
* Class: com_mine_dll_jni_TestDll
* Method: get
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_mine_dll_jni_TestDll_get
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
4、建立TestDll.cpp文件并引入TestDll.h文件 实现.h文件中的方法
#include "com_mine_dll_jni_TestDll.h"
int i = 0;
JNIEXPORT void JNICALL Java_com_mine_dll_jni_TestDll_set
(JNIEnv *, jclass, jint j){
i = j;
}
JNIEXPORT jint JNICALL Java_com_mine_dll_jni_TestDll_get
(JNIEnv *, jclass){
return i;
}
5、在vc6.0中创建win32 dynamic-link library工程,并在
tools->options->directories中添加
(1)C:\JAVA\JDK1.6.0_10\INCLUDE
(2)C:\JAVA\JDK1.6.0_10\INCLUDE\WIN32
不添加编译dll文件的时候会报找不到 "jni.h"
6、将生成的dll文件重命名java文件中loadLibrary函数中相同的名字
并放置与该java文件同一级目录,最后运行即可看见结果
7、如果是web项目 还必须将dll文件拷贝至%JAVA_HOME%BIN下
8、8、JSP 中的内容
<%@ page language="java" import="com.mine.dll.jni.*" %>
<body>
<%
System.out.println(System.getProperty( "java.library.path"));
TestDll d = new TestDll();
d.set(30);
out.println(d.get());
%>
</body>
public class TestDll {
static {
System.loadLibrary("testdll");
}
public native static void set(int i);
public native static int get();
public static void main(String[] args) {
TestDll d = new TestDll();
d.set(10);
System.out.println(d.get());
}
}
2、将该文件编译
/*
* javac -d . TestDll.java
* javah com.mine.dll.jni.TestDll
*/
3、javah com.mine.dll.jni.TestDll 生成TestDll.h文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_mine_dll_jni_TestDll */
#ifndef _Included_com_mine_dll_jni_TestDll
#define _Included_com_mine_dll_jni_TestDll
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_mine_dll_jni_TestDll
* Method: set
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_mine_dll_jni_TestDll_set
(JNIEnv *, jclass, jint);
/*
* Class: com_mine_dll_jni_TestDll
* Method: get
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_mine_dll_jni_TestDll_get
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
4、建立TestDll.cpp文件并引入TestDll.h文件 实现.h文件中的方法
#include "com_mine_dll_jni_TestDll.h"
int i = 0;
JNIEXPORT void JNICALL Java_com_mine_dll_jni_TestDll_set
(JNIEnv *, jclass, jint j){
i = j;
}
JNIEXPORT jint JNICALL Java_com_mine_dll_jni_TestDll_get
(JNIEnv *, jclass){
return i;
}
5、在vc6.0中创建win32 dynamic-link library工程,并在
tools->options->directories中添加
(1)C:\JAVA\JDK1.6.0_10\INCLUDE
(2)C:\JAVA\JDK1.6.0_10\INCLUDE\WIN32
不添加编译dll文件的时候会报找不到 "jni.h"
6、将生成的dll文件重命名java文件中loadLibrary函数中相同的名字
并放置与该java文件同一级目录,最后运行即可看见结果
7、如果是web项目 还必须将dll文件拷贝至%JAVA_HOME%BIN下
8、8、JSP 中的内容
<%@ page language="java" import="com.mine.dll.jni.*" %>
<body>
<%
System.out.println(System.getProperty( "java.library.path"));
TestDll d = new TestDll();
d.set(30);
out.println(d.get());
%>
</body>