您的位置:首页 > 编程语言 > Java开发

JAVA动态加载dll任意路径加载dll

2014-06-20 19:15 357 查看
package com.ctl.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.StringTokenizer;

public class SayHellotoCPP {
//LIBFILENAME dll文件的路径
static final String LIBFILENAME = "D:\\inca_workspace\\JNI\\bin\\HelloEnd.dll";// dll 文件
static {
try {
// 获取到java.library.path  及系统变量中Path中的内容
String libpath = System.getProperty("java.library.path");
if (libpath == null || libpath.length() == 0) {
throw new RuntimeException("java.library.path is null");
}
//javaBinPath   jdk的bin目录D:\Program Files\Java\jdk1.6.0_11\bin
String javaBinPath = null;
StringTokenizer st = new StringTokenizer(libpath,
System.getProperty("path.separator"));
if (st.hasMoreElements()) {
javaBinPath = st.nextToken();
} else {
throw new RuntimeException("can not split library path:"
+ libpath);
}
// 把dll文件写入到java.library.path中该dll放在ConvertWord2HM相同目录下,这个可以是你的类名
InputStream inputStream = SayHellotoCPP.class.getResourceAsStream(
LIBFILENAME);
final File dllFile = new File(LIBFILENAME);
if (!dllFile.exists()) {
FileOutputStream outputStream = new FileOutputStream(dllFile);
byte[] array = new byte[1024];
int bytesRead = -1;
while ((bytesRead = inputStream.read(array)) != -1) {
outputStream.write(array, 0, bytesRead);
}
outputStream.flush();
outputStream.close();
}
// 动态加载dll
System.load(dllFile.getPath());
// 在虚拟机关闭的时候删除dll
dllFile.deleteOnExit();
} catch (Throwable e) {
throw new RuntimeException("load Convert.dll error!", e);
}

}

public SayHellotoCPP() {
}

public native void sayHello(String name);

public static void main(String[] args) {
SayHellotoCPP shp = new SayHellotoCPP();
shp.sayHello("World");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: