首页 / JAVA / java:加载jar包中的动态库
java:加载jar包中的动态库
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java:加载jar包中的动态库,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3600字,纯文字阅读大概需要6分钟。
内容图文
java中System.load(String)方法
可以加载一个动态库,有时为了便于管理和发行,我们会把动态库打包jar包一起发行。这时如何加载jar包中的动态库呢?
原理也很简单,就是先把动态库解压到系统临时文件夹,再调用System.load(String)
方法加载动态库,github上这个项目native-utils上提供了完整实现代码,我做了一些简化,实现如下:
package net.gdface.cassdk;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
/**
* A simple library class which helps with loading dynamic libraries stored in the
* JAR archive. These libraries usually contain implementation of some methods in
* native code (using JNI - Java Native Interface).
*
* @see <a href="http://adamheinrich.com/blog/2012/how-to-load-native-jni-library-from-jar">http://adamheinrich.com/blog/2012/how-to-load-native-jni-library-from-jar</a>
* @see <a href="https://github.com/adamheinrich/native-utils">https://github.com/adamheinrich/native-utils</a>
*
*/
public class NativeUtils {
/**
* The minimum length a prefix for a file has to have according to {@link File#createTempFile(String, String)}}.
*/
private static final int MIN_PREFIX_LENGTH = 3;
public static final String NATIVE_FOLDER_PATH_PREFIX = "nativeutils";
/**
* Temporary directory which will contain the DLLs.
*/
private static File temporaryDir;
/**
* Private constructor - this class will never be instanced
*/
private NativeUtils() {
}
/**
* 从jar包中加载动态库
* 先将jar包中的动态库复制到系统临时文件夹,然后加载动态库,并且在JVM退出时自动删除。
* The file from JAR is copied into system temporary directory and then loaded. The temporary file is deleted after
* exiting.
* Method uses String as filename because the pathname is "abstract", not system-dependent.
*
* @param path 要加载动态库的路径,必须以'/'开始,比如 /lib/mylib.so,必须以'/'开始
* @param loadClass 用于提供{@link ClassLoader}加载动态库的类,如果为null,则使用NativeUtils.class
* @throws IOException 动态库读写错误
* @throws FileNotFoundException 没有在jar包中找到指定的文件
*/
public static synchronized void loadLibraryFromJar(String path, Class<?> loadClass) throws IOException {
if (null == path || !path.startsWith("/")) {
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
}
// Obtain filename from path
String[] parts = path.split("/");
String filename = (parts.length > 1) ? parts[parts.length - 1] : null;
// Check if the filename is okay
if (filename == null || filename.length() < MIN_PREFIX_LENGTH) {
throw new IllegalArgumentException("The filename has to be at least 3 characters long.");
}
// 创建临时文件夹
if (temporaryDir == null) {
temporaryDir = createTempDirectory(NATIVE_FOLDER_PATH_PREFIX);
temporaryDir.deleteOnExit();
}
// 临时文件夹下的动态库名
File temp = new File(temporaryDir, filename);
Class<?> clazz = loadClass == null ? NativeUtils.class : loadClass;
// 从jar包中复制文件到系统临时文件夹
try (InputStream is = clazz.getResourceAsStream(path)) {
Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
temp.delete();
throw e;
} catch (NullPointerException e) {
temp.delete();
throw new FileNotFoundException("File " + path + " was not found inside JAR.");
}
// 加载临时文件夹中的动态库
try {
System.load(temp.getAbsolutePath());
} finally {
// 设置在JVM结束时删除临时文件
temp.deleteOnExit();
}
}
/**
* 在系统临时文件夹下创建临时文件夹
*/
private static File createTempDirectory(String prefix) throws IOException {
String tempDir = System.getProperty("java.io.tmpdir");
File generatedDir = new File(tempDir, prefix + System.nanoTime());
if (!generatedDir.mkdir())
throw new IOException("Failed to create temp directory " + generatedDir.getName());
return generatedDir;
}
}
内容总结
以上是互联网集市为您收集整理的java:加载jar包中的动态库全部内容,希望文章能够帮你解决java:加载jar包中的动态库所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。