Check if a dll library is already loaded? (Java)

前端 未结 2 1719
闹比i
闹比i 2020-12-17 15:13

In a Java program i am writing i make a jni call to a dll and load the library on startup as follows

static
{
   System.loadLibrary(\"LdapAuthenticator2\");
         


        
相关标签:
2条回答
  • 2020-12-17 15:30

    What kind of an error? If it's an exception, can you just catch it?

    Another approach would be to make exactly one class responsible for loading the library. You could make loading the library part of the class's static initializer, and then loading the class == loading the library.

    EDIT: the javadocs for Runtime.loadLibrary() (which System.loadLibrary calls) even suggests the static initializer approach:

    If native methods are to be used in the implementation of a class, a standard strategy is to put the native code in a library file (call it LibFile) and then to put a static initializer:

         static { System.loadLibrary("LibFile"); }
    

    within the class declaration. When the class is loaded and initialized, the necessary native code implementation for the native methods will then be loaded as well.

    The javadocs also say:

    If this method is called more than once with the same library name, the second and subsequent calls are ignored.

    which makes me even more curious about the error you're getting.

    0 讨论(0)
  • 2020-12-17 15:44

    Check my answer to this question

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

    The solution works, unfortunately the poster of the question seems to have problems with a non SUN compatible JVM or a too restrictive SecurityManager.

    Link to the sample POC source code.

    List loaded JNI libraries java sourcecode

    0 讨论(0)
提交回复
热议问题