有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

检查是否已加载dll库?(爪哇)

在我正在编写的Java程序中,我对dll进行jni调用,并在启动时加载库,如下所示

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

然后,我实现了另一个类,该类加载了同一个库,并且得到一个错误,表示库已经加载,是否有方法检查库是否已经运行

谢谢,
-皮特


共 (2) 个答案

  1. # 1 楼答案

    什么样的错误?如果这是个例外,你能抓住它吗

    另一种方法是让一个类负责加载库。您可以使加载库成为类的静态初始值设定项的一部分,然后加载类==加载库

    编辑:javadocs for ^{}(由System.loadLibrary调用)甚至建议使用静态初始值设定方法:

    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.

    javadocs还说:

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

    这让我对你的错误更加好奇