免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1299 | 回复: 0
打印 上一主题 下一主题

一个JNI的小例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-07 13:47 |只看该作者 |倒序浏览
最近要在JAVA程序里调用C++写的.dll文件,要用到JNI技术,找了些资料,自己做了个小例子.步骤如下:
JAVA 代码:
public class testdll{
static {
  System.loadLibrary("DLL");
}
public native static int get();
public native static void set(int i);
public static void main(String[] args) {
testdll test = new testdll();
test.set(100);

System.out.println(test.get());
}
}

编译,然后使用javah -jni testdll生成testdll.h文件

C代码:
#include"testdll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
int i = 0;
JNIEXPORT jint JNICALL Java_testdll_get (JNIEnv *, jclass)
{
return i;
}
JNIEXPORT void JNICALL Java_testdll_set (JNIEnv *, jclass, jint j)
{
i = j;
}

如果有如下错误:
fatal error C1083: Cannot open include file: 'jni.h': No such file or directory
将以下文件:
\jdk\include\jni.h
\jdk\include\win32\jawt_md.h
\jdk\include\win32\jni_md.h
复制到Visual Studio.net的安装目录下的\Vc7\include目录中
如果是VC6.0,自己找相应的目录即可!
然后编译,成功后,将生成的.dll文件copy到JAVA文件的同一级目录中
运行java程序````````




本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/15642/showart_151948.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP