java 调用dll文件总结
2009-07-22 02:36
253 查看
java 调用dll文件时
几个注意点:
1. package的使用
2. javah的使用
3.path路径的设定
下面实例介绍java调用dll中的Max函数:
hello.java
package 2hei.net.dll;
public class hello
{
static
{
//System.out.println(System.getProperties().get("java.library.path"));
System.loadLibrary("Hello");
}
public native static int Max(int a,int b);
public static void main(String[] args)
{
int maxnum = 0;
int aa = 10;
int bb = 11;
hello hi= new hello();
maxnum = hi.Max(aa,bb);
System.out.println("max is "+maxnum);
}
}
生成.h头文件
createh.bat
cd E:/src/java/2hei/net/dll
javah hello
会生成一个2hei_net_dll_hello.h的文件
编辑编辑 2hei_net_dll_hello.h 把#include <jni.h> 改成#include "jni.h"
从jdk的目录里面找到jni.h 和 jni_md.h
下面使用VC++生成dll文件。
新建一个dll工程,比如Hello 编辑Hello.cpp
// Hello.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "Hello.h"
#include "2hei_net_dll_hello.h"
JNIEXPORT jint JNICALL 2hei_net_dll_hello_Max
(JNIEnv *, jclass, jint a, jint b)
{
if(a>=b)return a;
else
return b;
}
编译工程后,在Debug目录中找到Hello.dll文件,放到java的path目录下面。
执行hello.java 即可以得到想要的结果。
几个注意点:
1. package的使用
2. javah的使用
3.path路径的设定
下面实例介绍java调用dll中的Max函数:
hello.java
package 2hei.net.dll;
public class hello
{
static
{
//System.out.println(System.getProperties().get("java.library.path"));
System.loadLibrary("Hello");
}
public native static int Max(int a,int b);
public static void main(String[] args)
{
int maxnum = 0;
int aa = 10;
int bb = 11;
hello hi= new hello();
maxnum = hi.Max(aa,bb);
System.out.println("max is "+maxnum);
}
}
生成.h头文件
createh.bat
cd E:/src/java/2hei/net/dll
javah hello
会生成一个2hei_net_dll_hello.h的文件
编辑编辑 2hei_net_dll_hello.h 把#include <jni.h> 改成#include "jni.h"
从jdk的目录里面找到jni.h 和 jni_md.h
下面使用VC++生成dll文件。
新建一个dll工程,比如Hello 编辑Hello.cpp
// Hello.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "Hello.h"
#include "2hei_net_dll_hello.h"
JNIEXPORT jint JNICALL 2hei_net_dll_hello_Max
(JNIEnv *, jclass, jint a, jint b)
{
if(a>=b)return a;
else
return b;
}
编译工程后,在Debug目录中找到Hello.dll文件,放到java的path目录下面。
执行hello.java 即可以得到想要的结果。
相关文章推荐
- java调用quartz问题总结
- paip.java 调用c++ dll so总结
- Axis2学习总结(2)-----通过wsdl2java工具调用第三方服务
- JNI学习总结之:java调用native函数以及native函数回调java类成员方法
- 纯Java版工行B2C网银接口调用总结
- 用java调用oracle存储过程总结
- java 调用webservice的各种方法总结
- Java 调用webservice的各种方法总结
- Android平台下使用lua调用Java代码经验总结
- Java调用WebService的各种方法总结(转载)
- 关于Java对相对目录的文件读写和调用方法的一些总结
- java 调用webservice的各种方法总结
- Java【多线程知识总结(2)】调用setDaemon(true)变成后台线程
- 1.23 Java基础总结 方法的声明和调用
- java调用Kettle总结
- 用java调用oracle存储过程总结
- java 调用webservice的各种方法总结
- 用java调用oracle存储过程总结
- Atitit.java jna 调用c++ dll 的总结
- java调用com组件操作word使用总结(jacob)