在Java中在运行时为本机库添加新路径

是否可以在运行时为本机库添加新路径? (而不是使用属性java.library.path启动Java),因此在尝试查找nativeLibraryName时,对System.loadLibrary(nativeLibraryName)的调用将包含该路径。 这是可能的,或者这些路径在JVM启动后被冻结了吗?

没有少量黑客攻击似乎是不可能的(即访问ClassLoader类的私有字段)

这个博客提供了两种方法。

记录中,这是简短版本。

选项1:用新值完全替换java.library.path

 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的新路径

 /** * 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); }