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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1139386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:11:26  来源:igfitidea点击:

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

javajava-native-interface

提问by Petey B

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

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

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

I then implemented another class that loads the same library and am getting an error saying that the library is already loaded, is there any way to check if the library is already running?

然后我实现了另一个加载同一个库的类,并收到一个错误,说库已经加载,有没有办法检查库是否已经在运行?

Thanks,
-Pete

谢谢,-
皮特

采纳答案by jitter

Check my answer to this question

检查我对这个问题的回答

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

如何获取加载的 JNI 库列表?

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

该解决方案有效,不幸的是,问题的发布者似乎在不兼容 SUN 的 JVM 或过于严格的 SecurityManager 方面存在问题。

Link to the sample POC source code.

链接到示例 POC 源代码。

List loaded JNI libraries java sourcecode

List loaded JNI libraries java sourcecode

回答by Laurence Gonsalves

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.loadLibrarycalls) even suggests the static initializer approach:

编辑:针对的javadocRuntime.loadLibrary()(其中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.

如果要在类的实现中使用本机方法,标准策略是将本机代码放入库文件中(称为 LibFile),然后放入静态初始化程序:

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

在类声明中。当类被加载和初始化时,本地方法的必要本地代码实现也将被加载。

The javadocs also say:

javadocs 还说:

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.

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