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?
java. library. path is a System property, which is used by Java programming language, mostly JVM, to search native libraries, required by a project.
JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).
A native library is a library that contains "native" code. That is, code that has been compiled for a specific hardware architecture or operating system such as x86 or windows. Including such native library in your project may break the platform-independence of you application.
The JNI header " jni. h " provided by JDK is available under the " <JAVA_HOME>\include " and " <JAVA_HOME>\include\win32 " (for Windows) or " <JAVA_HOME>\include\linux " (for Ubuntu) [Check Mac OS X] directories, where <JAVA_HOME> is your JDK installed directory (e.g., " c:\program files\java\jdk10.
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 from here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With