How do I get a list of JNI libraries which are loaded?

[DISCLAIMER: Note that this solution always was hackish. And in most cases won’t work anymore nowadays. Check Benjamins answer for more info]


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.

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[] {});
    }
}

Call the above like this

final String[] libraries = ClassScope.getLoadedClasses(ClassLoader.getSystemClassLoader()); //MyClassName.class.getClassLoader()

And voilá libraries holds the names of the loaded native libraries.

Get the full source code here and here

Leave a Comment