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

2022-01-25 00:00:00 native java java-native-interface

是否可以在运行时为本地库添加新路径?(而不是使用属性 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);
}

相关文章