Adding new paths for native libraries at runtime in Java(在 Java 运行时为本地库添加新路径)
问题描述
是否可以在运行时为本地库添加新路径?(而不是使用属性 java.library.path 启动 Java),因此在尝试查找 nativeLibraryName
时,对 System.loadLibrary(nativeLibraryName)
的调用将包含该路径.是否有可能,或者一旦 JVM 启动,这些路径就会被冻结?
Is it possible to add a new path for native libraries at runtime ?.
(Instead of starting Java with the property java.library.path), so a call to System.loadLibrary(nativeLibraryName)
will include that path when trying to find nativeLibraryName
.
Is that possible or these paths are frozen once the JVM has started ?
推荐答案
[此解决方案不适用于 Java 10+]
如果没有很少的黑客攻击,这似乎是不可能的(即访问 ClassLoader 类的私有字段)
It seems impossible without little hacking (i.e. accessing private fields of the ClassLoader class)
这个 博客 提供 2方法.
This blog provide 2 ways of doing it.
为了记录,这里是简短的版本.
For the record, here is the short version.
选项 1: 将 java.library.path 完全替换为新值)
Option 1: fully replace java.library.path with the new value)
public static void setLibraryPath(String path) throws Exception {
System.setProperty("java.library.path", path);
//set sys_paths to null so that java.library.path will be reevalueted next time it is needed
final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
sysPathsField.setAccessible(true);
sysPathsField.set(null, null);
}
选项 2:向当前 java.library.path 添加新路径
Option 2: add a new path to the current java.library.path
/**
* Adds the specified path to the java library path
*
* @param pathToAdd the path to add
* @throws Exception
*/
public static void addLibraryPath(String pathToAdd) throws Exception{
final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
usrPathsField.setAccessible(true);
//get array of paths
final String[] paths = (String[])usrPathsField.get(null);
//check if the path to add is already present
for(String path : paths) {
if(path.equals(pathToAdd)) {
return;
}
}
//add the new path
final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
newPaths[newPaths.length-1] = pathToAdd;
usrPathsField.set(null, newPaths);
}
这篇关于在 Java 运行时为本地库添加新路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 运行时为本地库添加新路径
基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01