Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 879
benhsu Avatar asked Jun 17 '09 15:06

benhsu


People also ask

What is Djava library path?

java. library. path is a System property, which is used by Java programming language, mostly JVM, to search native libraries, required by a project.

What is a JNI library?

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++).

What are native system libraries?

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.

Where is JNI?

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.


1 Answers

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

like image 160
jitter Avatar answered Jan 06 '23 08:01

jitter