Native Library already loaded in another classloader [duplicate]
Asked Answered
F

2

6

I need some help to handle following scenario.

I am using two applets which requires the same native library (.dll) file.

So when I run the applets from the web pages, for the first time first applet loads the dll into the applet class loader. It works fine. But when second applet tries to load the same dll it gives me exception saying that "Error loading win32com: java.lang.UnsatisfiedLinkError: Native Library C:\WINDOWS\system32\win32com.dll already loaded in another classloader"

I using following method to load the driver.

CommDriver driver = (CommDriver)Class.forName("com.sun.comm.Win32Driver").newInstance();
driver.initialize();

Please give me the solution

Thanks & Rgds, Rishikesh

Feticide answered 16/9, 2010 at 7:8 Comment(1)
The solution is given here #1139886 and there #1031292Therapy
S
0

In short, the same instance of the class that uses the native library must be shared by both classes.

You can do this by getting the system class loader (or the top parent of the class loader of your current class) and then dynamically have it load the class which uses the native library.

If you don't know which class does load the native library then you can make a proxy class and load this isntead. The class must call to the other libraries for you so they will be loaded with the Proxy classes classloader (and so also be shared).

However I would expect the security manager to prevent you from doing this within an Applet.

Schermerhorn answered 16/9, 2010 at 8:55 Comment(1)
Hi Thanks for your quick assistance.I Used System.loadLibrary("win32com"); but i am getting the follwing exeption :javax.comm.NoSuchPortException at javax.comm.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:105) Hence i load the driver using CommDriver driver = (CommDriver)Class.forName("com.sun.comm.Win32Driver").newInstance(); driver.initialize(); Is there any example of code where I can refer shared library stuff?Feticide
M
0

There is no point in loading the Native Library(as a matter of fact any library)twice in different locations. You can make Applet1's *driver* object as static.
And use it in Applet2.

Monterrey answered 1/4, 2013 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.