Native Library already loaded in another classloader [duplicate]

给你一囗甜甜゛ 提交于 2019-12-01 03:28:24

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3724421/native-library-already-loaded-in-another-classloader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!