Solve the problem of "Java. Lang. unsatisfiedlinkerror: Native library. dll already loaded in another classloader"

Source: Internet
Author: User

I encountered this problem some time ago, that is, using the same Applet in two projects, and then calling the DLL operation (using JNI or JNA) in the applet ), then, call the API on the client. The actual access process is as follows:
First, access an interface of Project A. appleta is called on the interface, and then access the interface of Project B without closing the browser, and appletb is called on the interface. Appleta and appletb are actually the same applet, but this applet is used in two projects and both projects are directly accessed. At this time, an error occurs when accessing appletb:

 
Xxx not loaded java. Lang. unsatisfiedlinkerror: Native library XXX. dll already loadedInAnother classloader

If you access from appletb to appleta, the same error occurs when you access appleta.
Because in one tab, multiple applets are actually running on the same JVM, but different classloader is used when the applet is loaded. Therefore, whether appleta runs first or appletb runs first, the DLL on which it depends will be loaded by the same JVM, and the above error will occur.

After Google, many developers encountered the same problem, some of which were caused by the same javaee container (such as Weblogic and JBoss) both of them have been deployed to access the same JNI called Project, and some have the same experience as the author. The final conclusion is that a DLL cannot be loaded twice in a JVM. Therefore, when calling JNI later, try to load the same DLL again. At this time, the above error will be reported. Because of this error, the corresponding Java class cannot be initialized, so the corresponding project or applet cannot be started.

It is actually very easy to solve this problem.CodeThe classloader of the project is not directly loaded, but is loaded by systemloader. One way is to encapsulate this part of code into a jar and put it in a place where Java systemloader can be loaded, such as the LIB/EXT directory. The code is still called in the project. Because the code for accessing the DLL is loaded by systemloader, when multiple projects access the same DLL at the same time, you can avoid re-loading. Because the class found during access to the second project has been loaded by systemloader, so the classloader of the project itself will not load the class.

Compared to the original appleta and appletb, the modified structure is as follows: appleta, appletb, and jniaccess. Jar. Place jniaccess. jar in the ext directory of the lib directory of JRE. In this way, you can access the applet again.

Http://www.iflym.com/index.php/code/resolve-java-lang-unsatisfiedlinkerror-native-library-dll-already-loaded-in-another-classloader-problem.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.