Analysis of DLL file dynamic loading method in Java Web project (detailed steps)

I believe that many Java friends have experienced calling C or C + + methods by calling JNI in Java, so how to realize the dynamic loading method of DLL / so files in Java Web. Today I will bring you an article on the dynamic loading method of DLL / so file in Java Web project.

In Java Web projects, we often use JNI to call DLL dynamic library files to realize some functions that Java can not achieve, or some third-party DLL plug-ins. It is common practice to copy these DLL files to% Java_ Home% \ JRE \ bin \ folder or application middleware (tomcat|weblogic) can only be used normally in the program after it is in the bin directory. However, this step is often forgotten during project implementation or migration, which is troublesome. Therefore, we consider whether it can be loaded dynamically during project operation without manually copying these files. We have found a lot of information on the Internet, and most of them are either manually copied or Java Solutions in the project (not applicable to Java Web projects), so sort out this material for reference.

The first step is to create a listening class

The purpose of establishing a listening class is to automatically execute the loader when the application middleware is started.

1) Create a class to implement the servletcontextlistener interface

2) Implement the contextinitialized method

3) On the project's web Configure this listening class in the XML file

Step 2 add the path where the DLL file is located to the system environment Java library. In path

The addition process needs to use the reflection mechanism. You cannot use system Setproperty() is set, which is also invalid. Look directly at the code:

Step 3 load DLL file

Next, you can write the method of context initialization:

Step 4 restart and start the application middleware (tomcat|weblogic)

At this point, you can use the method of DLL file in your Java program.

The above is all about the dynamic loading method of DLL file in Java Web project. I hope it will be helpful to you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>