Search code examples
javaappletjava-native-interfaceclassloader

Java applet issue: dll already loaded in another classloader


I need to create a Java applet which loads a local DLL. It works fine, but only the first time. If I refresh the page, it always complains by throwing the following exception:

Exception: java.lang.RuntimeException: java.lang.UnsatisfiedLinkError: Native Library E:\test.dll already loaded in another classloader

Any ideas on how I can fix this?

Here is my code:

public class NativeWrapper
{
    public native String GetIP();

    public NativeWrapper(final String nativeLib) {
        try {
            System.load(nativeLib);
        } catch (UnsatisfiedLinkError e) {
            System.out.println("UnsatisfiedLinkError exception" + e);
        }
    }
}

And the applet code:

public class MyApplet extends Applet {

    private static NativeWrapper dll = new NativeWrapper("e:/test.dll");

    public MyApplet () {
    }

    // to be called by javascript on html page
    public string GetIPAddress() {
        return dll.GetIP();
    }
}

I have read a lot on this issue but still can't find a solution. Can anyone help? Thanks in advance.


Solution

  • This article seems to provide some good advise to make sure you get the same ClassLoader for each applet invocation.