如何避免两次调用System.load? [英] How can I avoid calling System.load twice?

查看:1920
本文介绍了如何避免两次调用System.load?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类调用本机函数来从CMOS获取有关系统的信息。该类有一个静态初始化块,它加载包含本机函数的库,它看起来像这样:

I have a class that calls a native function to get information about a system from its CMOS. The class has a static initialization block which loads the library containing the native function, and it looks something like this:

package lib.sysid;

public class SysId
{
    private static native int getSysIdNative();
    private static final String SYS_ID_PATH = "libsysid.so";

    static
    {
        System.load(SYS_ID_PATH);
    }

    public static int getSysIdFromCMOS()
    {
        int returnValue = getSysIdNative();
    }
}

根据我的测试,该方法工作正常我使用它的时间,但如果我稍后再次调用该方法,静态初始化块也会运行,导致UnsatisfiedLinkError:

According to my testing, the method works fine the first time I use it, but if I call the method again at a later time, the static initialization block also runs, causing an UnsatisfiedLinkError:

java.lang.UnsatisfiedLinkError: Native Library libsysid.so already loaded in another classloader

我该怎么办?避免静态初始化块执行 System.load()方法(如果已经运行)?

How can I avoid the static initialization block from executing the System.load() method if it has already been run?

或者,有没有办法让我尝试卸载库,如果它已经加载,再次调用 System.load()方法之前?

Alternatively, is there a way for me to attempt to "unload" the library if it is already loaded before calling the System.load() method again?

编辑:奇怪的是,如果我用try-catch块包围 System.load()调用,我仍然会收到UnsatisfiedLinkError,但这一次它来自对 getSysIdNative()的实际调用。我看到的错误如下:

Strangely enough, if I surround the System.load() call with a try-catch block, I still get an UnsatisfiedLinkError, but this time it comes from the actual call to getSysIdNative(). The error I see is the following:

lib.sysid.SysId.getSysIdNative()I

I出现了什么?我试图将调试器附加到此代码以查看消息的填充位置,但到目前为止我还没有成功。

What the heck is that "I" that shows up? I've tried to attach a debugger to this code to see where the message gets populated, but so far I haven't been successful.

推荐答案

只是一个猜测,但我认为单个JVM加载类(并执行其静态初始化程序)两次的唯一方法是使用不同的类加载器加载它。所以这里可能有第二个类加载器,你不知道。如果第二次有不同的(一组)类加载器生效,这将适用。

Just a guess, but I think the only way for a single JVM to load a class (and execute its static initializers) twice is to load it with different classloaders. So there may be a second classloader involved here that you're not aware of. This would apply if a different (set of) classloader(s) is in effect the second time around.

在真实操作系统下, java -verbose:class 会给你加载器消息以验证这一点。我不确定如何在嵌入式系统上验证这一点。您可以修改 getSysId()以打印(?)或以某种方式转储对的引用SysId.class.getClassLoader()

Under a "real" operating system, java -verbose:class would give you loader messages to verify this with. I'm not sure how you'd go about verifying this on an embedded system. You could modify getSysId() to print (?) or somehow dump a reference to SysId.class.getClassLoader().

这篇关于如何避免两次调用System.load?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆