JNI WEB项目调用DLL文件

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>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值