如何获取已加载的JNI库的列表? [英] How do I get a list of JNI libraries which are loaded?
问题描述
主题只是在说什么,Java中是否有一种方法可以获取在任何给定时间已加载的所有JNI本机库的列表?
Just what the subject says, is there a way in Java to get a list of all the JNI native libraries which have been loaded at any given time?
推荐答案
如果您愿意的话,可以使用一种方法来确定所有当前加载的本机库.无法确定已卸载的库.
There is a way to determine all currently loaded native libraries if you meant that. Already unloaded libraries can't be determined.
Based on the work of Svetlin Nakov (Extract classes loaded in JVM to single JAR) I did a POC which gives you the names of the loaded native libraries from the application classloader and the classloader of the current class.
首先是没有错误的简化版本....它的异常处理,良好的错误消息,javadoc,....
First the simplified version with no bu....it exception handling, nice error messages, javadoc, ....
获取私有字段,类加载器通过反射将已加载的库存储在该私有字段中
Get the private field in which the class loader stores the already loaded libraries via reflection
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()); //MyClassName.class.getClassLoader()
voilá libraries
保存已加载的本机库的名称.
And voilá libraries
holds the names of the loaded native libraries.
从此处
这篇关于如何获取已加载的JNI库的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!