先说一下需求吧,

目前在用JCEF实现java程序桌面版包装,源码中需要加载编译好的几个dll文件,而这些文件的路径必然是根据程序安装的路径而变化的,这就需要在程序运行的时候,去动态修改java.library.path来实现dll文件的加载

最开始的时候,我想到了System.setProperty();但是不管怎么试,都没办法动态加载进来,后来google了一下,发现这个方法需要重启JVM才行,原因看下面这段代码:

if (sys_paths == null) {
usr_paths = initializePath("java.library.path");
sys_paths = initializePath("sun.boot.library.path");
}

这段代码在ClassLoader类,loadLibrary方法中,这段代码的意思很好理解,sys_paths为null的时候会去加载一次变量,也就是说JVM启动后就不可能再去加载,既然不能直接用System.setProperty();那就只能另寻他路,我开始在网上找资料,不难找到很多博客(原创或转载)提供了一种方法,代码如下:

 private static void addLibraryDir(String libraryPath) throws Exception {
Field field = ClassLoader.class.getDeclaredField("usr_paths");
field.setAccessible(true);
String[] paths = (String[]) field.get(null);
for (int i = 0; i < paths.length; i++) {
if (libraryPath.equals(paths[i])) {
return;
}
}
String[] tmp = new String[paths.length + 1];
System.arraycopy(paths, 0, tmp, 0, paths.length);
tmp[paths.length] = libraryPath;
field.set(null, tmp);
}

我用这种方法,把我的路径加进去了,但是在实际加载的时候,仍然找不到,这就有点尴尬了,我怀疑是不是自己的代码出错了,还是加路径的位置不对,但是尝试了N久,该方法始终无法跑通,也许是小弟资质略差,如有幸有大神看到此处,还望指点迷津


既然此路不通,咱也不能在一棵树上吊死不是,另寻他路,英文蹩脚的我无奈实处必杀技,在google上开始搜索英文资料,google搜索 “java modify java.library.path at runtime” 没想到第一条搜索就解决了我的问题!!!!!!

changing-java-library-path-at-runtime

该博客的第一个方法,还是最上面那两行代码,还是那个位置,该博客博主的方法就是设置sys_paths为null,既然每次只有在null的时候会初始化,那你何不设置路径后,重新置为null,让他下次执行的时候继续初始化呢?年少的我还是有很多路要走啊,这么看似简单的想法,居然一直没有想到,惭愧...

借鉴该方法,最后代码如下:

private static void addLibraryDir(String libraryPath) throws Exception {
Field userPathsField = ClassLoader.class.getDeclaredField("usr_paths");
userPathsField.setAccessible(true);
String[] paths = (String[]) userPathsField.get(null);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < paths.length; i++) {
if (libraryPath.equals(paths[i])) {
continue;
}
sb.append(paths[i]).append(';');
}
sb.append(libraryPath);
System.setProperty("java.library.path", sb.toString());
final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
sysPathsField.setAccessible(true);
sysPathsField.set(null, null);
}

好了,问题完美解决,心中一块石头落地,中午又可以多吃两块肉了,哈哈~~

该文章最新发表于CSDN博客,后为推广,在博客园发表一份,文章为原创兼翻译,转载请注明出处,谢谢!

java如何修改java.library.path并且不重启jvm也能生效的更多相关文章

  1. 怎样解决 no jzmq in java.library.path

    1. 确保zmq的各种library有安装正确.检查方法:查看/usr/local/lib, 看里面有没有libjzmq.a, libjzmq.dylib, libzmq.a, libjzmq.dyl ...

  2. paip.-Djava.library.path -Djava.ext.dirs= 的区别

    paip.-Djava.library.path  -Djava.ext.dirs= 的区别 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专 ...

  3. IDEA搭建ssm框架测试衍生出的问题The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\Develop\jdk7\jdk1.7.0_79\bin;

    最近玩起IDEA这开发工具,搭建ssm框架测试时,部署项目出现如下问题: 信息: The APR based Apache Tomcat Native library which allows opt ...

  4. Tomcat启动慢原因之二 he APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path

    Tomcat启动时提示: 信息: The APR based Apache Tomcat Native library which allows optimal performance in prod ...

  5. Exception in thread "main" java.lang.UnsatisfiedLinkError: no awt in java.library.path:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no awt in java.library.path: 这是 ...

  6. The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path 问题解决记录

    1.问题 启动Tomcat之后,在浏览器输入IP后显示503,查看catalina.log发现报错: 2.问题定位:缺少 tomcat-native library 就是说 缺少Tomcat Nati ...

  7. no leveldbjni64-1.8 in java.library.path

    在抽取以太坊Java版本的Trie树部分时,遇到了一个问题: Exception in thread "main" java.lang.UnsatisfiedLinkError: ...

  8. no sigar-amd64-winnt.dll in java.library.path 错误

    需要维护别人写的一个WEB项目,还原数据库,从SVN中检出源码,运行,提示如下错误: 5526 [localhost-startStop-1] DEBUG Sigar  - no sigar-amd6 ...

  9. 关于Tomcat启动时报The APR based Apache Tomcat Native library which allows optimal performanc e in production environments was not found on the java.library.path

    错误信息如下 八月 01, 2016 10:11:15 上午 org.apache.catalina.core.AprLifecycleListener initINFO: The APR based ...

随机推荐

  1. solr5.2 mysql 增量索引

    前提:数据库里数据进行增删改操作时,相应的solr需要修改或者新建索引,之前从数据库中导入数据并创建索引的操作是全量创建,如果本身数据库数据量非常大,就需要增量创建索引 1./usr/local/sr ...

  2. http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/index.html

    http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/index.html

  3. scala中的Actor

    1.介绍 2.简单示例 3.第二个程序 4.通信程序 package day01 import scala.actors.Actor case class Message(content: Strin ...

  4. asio制作使用ssl通信的证书

    1,生成ca的keyopenssl genrsa -out ca.key 1024/2048 (with out password protected) openssl genrsa -des3 -o ...

  5. IIS 7.5 + PHP-5.6.3 + mysql-5.6.21.1

    禅道项目管理软件源码下载:http://sourceforge.net/projects/zentao/files/6.3/ZenTaoPMS.6.3.stable.zip/download Stp1 ...

  6. Javascript Promise入门

    是什么? https://www.promisejs.org/ What is a promise? The core idea behind promises is that a promise r ...

  7. Testlink部署全攻略

    部署前准备: xampp,我下载的链接:https://www.apachefriends.org/download.html Testlink,下载地址:https://sourceforge.ne ...

  8. C# ref、out、params与值类型参数修饰符

    1.值类型: static void Main(string[] args) { ; ; NumVal(a, b); Console.WriteLine("a={0},b={1}" ...

  9. Coins

    Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hi ...

  10. [LeeCode]Power of Two

    Given an integer, write a function to determine if it is a power of two. My initial code: class Solu ...