如何在 JDK 17 应用程序中查找通过 JNI 使用的本机库?

问题描述 投票:0回答:0

我分析了我的 Java 应用程序并知道它在本机代码中包含内存泄漏,但在应用程序自己的 Java 源代码中没有。我正在尝试确定可能有问题的依赖项或依赖项。

回到 JDK 的早期版本,你可以在 Java 应用程序中找到像这样的本地库:

public class ClassScope {
    private static final java.lang.reflect.Field LIBRARIES;
    static {
        LIBRARIES = ClassLoader.class.getDeclaredField("loadedLibraryNames");
        LIBRARIES.setAccessible(true);
    }
    public static String[] getLoadedLibraries(final ClassLoader loader) {
        final Vector<String> libraries = (Vector<String>) LIBRARIES.get(loader);
        return libraries.toArray(new String[] {});
    }
}

final String[] libraries = ClassScope.getLoadedClasses(ClassLoader.getSystemClassLoader()); 

但是,如果你在 9 之后的 JDK 上尝试这个,你会收到警告,这个访问权限将从 Java 12 中撤销:


WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by <class> (file:<file>) to method|constructor
WARNING: Please consider reporting this to the maintainers of <file>
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

在 JDK 17 中,该字段已完全删除:

No field loadedLibraryNames in class Ljava/lang/ClassLoader;

那么,在 JDK 17 应用程序中,有哪些选项可以识别通过 JNI 使用的本机库?

java java-native-interface
© www.soinside.com 2019 - 2024. All rights reserved.