解决JNA动态加载jar中dll问题
2016-05-09 00:00
393 查看
摘要: 在使用jna过程中,需要将项目导出为jar包运行,解决了加载第三方jar的问题,但运行时还是报错,不能加载dll。
问题分析:将项目导出为jar文件后,dll文件的路径已经改变,所以在eclipse中项目可以正常运行,但运行jar文件将报无法加载dll的错误。
使用jna加载库文件使用如下方式: Native.loadLibrary(dllName, className);
loadLibrary源码:
public static Object loadLibrary(String name, Class interfaceClass) {
return loadLibrary(name, interfaceClass, Collections.EMPTY_MAP);
}
即使用dll的名字加载库文件,不能带后缀。
在不修改代码代码的前提下,那么项目该如何适应dll文件路径的动态变化呢,一方面能够在eclipse中正常运行,导出jar后能在生产环境执行。
下面我写了一个方法解决这个问题:
public synchronized static Object loadDll(String libName, Class<?> className) {
String systemType = System.getProperty("os.name");
String libExtension = (systemType.toLowerCase().indexOf("win") != -1) ? ".dll"
: ".so";
String libFullName = libName + libExtension;
String nativeTempDir = System.getProperty("java.io.tmpdir");
InputStream in = null;
BufferedInputStream reader = null;
FileOutputStream writer = null;
File extractedLibFile = new File(nativeTempDir + File.separator
+ libFullName);
if (!extractedLibFile.exists()) {
try {
in = className.getResourceAsStream("/" + libFullName);
if (in == null)
in = className.getResourceAsStream(libFullName);
reader = new BufferedInputStream(in);
writer = new FileOutputStream(extractedLibFile);
byte[] buffer = new byte[1024];
while (reader.read(buffer) > 0) {
writer.write(buffer);
buffer = new byte[1024];
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
if (writer != null)
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String dllName;
if (systemType.toLowerCase().indexOf("win") != -1) {
dllName = extractedLibFile.toString().replace(".dll", "");
} else {
dllName = extractedLibFile.toString().replace(".so", "");
}
return Native.loadLibrary(dllName, className);
}
加载libHCEHomeCMS,调用例子:
HCEHomeCMS INSTANCE = (HCEHomeCMS) PropertiesUtils.loadDll("libHCEHomeCMS",HCEHomeCMS.class);
到此,整个项目可以使用动态方式加载dll,运行jar也能正常加载,有个问题需要注意下,32位dll文件需要使用32位jdk才能加载。
欢迎指出本文有误的地方,转载请注明原文出处https://my.oschina.net/7001/blog/672173
问题分析:将项目导出为jar文件后,dll文件的路径已经改变,所以在eclipse中项目可以正常运行,但运行jar文件将报无法加载dll的错误。
使用jna加载库文件使用如下方式: Native.loadLibrary(dllName, className);
loadLibrary源码:
public static Object loadLibrary(String name, Class interfaceClass) {
return loadLibrary(name, interfaceClass, Collections.EMPTY_MAP);
}
即使用dll的名字加载库文件,不能带后缀。
在不修改代码代码的前提下,那么项目该如何适应dll文件路径的动态变化呢,一方面能够在eclipse中正常运行,导出jar后能在生产环境执行。
下面我写了一个方法解决这个问题:
public synchronized static Object loadDll(String libName, Class<?> className) {
String systemType = System.getProperty("os.name");
String libExtension = (systemType.toLowerCase().indexOf("win") != -1) ? ".dll"
: ".so";
String libFullName = libName + libExtension;
String nativeTempDir = System.getProperty("java.io.tmpdir");
InputStream in = null;
BufferedInputStream reader = null;
FileOutputStream writer = null;
File extractedLibFile = new File(nativeTempDir + File.separator
+ libFullName);
if (!extractedLibFile.exists()) {
try {
in = className.getResourceAsStream("/" + libFullName);
if (in == null)
in = className.getResourceAsStream(libFullName);
reader = new BufferedInputStream(in);
writer = new FileOutputStream(extractedLibFile);
byte[] buffer = new byte[1024];
while (reader.read(buffer) > 0) {
writer.write(buffer);
buffer = new byte[1024];
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
if (writer != null)
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String dllName;
if (systemType.toLowerCase().indexOf("win") != -1) {
dllName = extractedLibFile.toString().replace(".dll", "");
} else {
dllName = extractedLibFile.toString().replace(".so", "");
}
return Native.loadLibrary(dllName, className);
}
加载libHCEHomeCMS,调用例子:
HCEHomeCMS INSTANCE = (HCEHomeCMS) PropertiesUtils.loadDll("libHCEHomeCMS",HCEHomeCMS.class);
到此,整个项目可以使用动态方式加载dll,运行jar也能正常加载,有个问题需要注意下,32位dll文件需要使用32位jdk才能加载。
欢迎指出本文有误的地方,转载请注明原文出处https://my.oschina.net/7001/blog/672173
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树