Examples of methods for dynamically loading dll files in JavaWeb projects
这篇文章主要介绍了JavaWeb项目中dll文件动态加载方法,步骤详细,在这里分享给大家,需要的朋友可以了解下。
相信很多做Java的朋友都有过用Java调用JNI实现调用C或C++方法的经历,那么Java Web中又如何实现DLL/SO文件的动态加载方法呢。今天就给大家带来一篇JAVA Web项目中DLL/SO文件动态加载方法的文章。
在Java Web项目中,我们经常会用到通过JNI调用dll动态库文件来实现一些JAVA不能实现的功能,或者是一些第三方dll插件。通常的做法是将这些dll文件复制到 %JAVA_HOME%\jre\bin\ 文件夹或者 应用中间件(Tomcat|Weblogic)的bin目录下之后,在程序中才能正常使用。但是这个步骤在项目实施或移植时经常会被忘记,比较麻烦,所以就考虑能否在项目运行过程中动态加载,而不需要再手动复制这些文件。在网上找了很多资料,大部分的说法要么是手动复制,要么就是JAVA项目中的解决方法(不适用于JAVA Web项目),所以整理此资料,以供参考。
第一步 建立一个监听类
建立监听类的作用是在应用中间件启动时自动执行加载程序。
1)创建一个类实现ServletContextListener 接口
2)实现contextInitialized方法
3)在项目的web.xml 文件中配置此监听类
第二步 将dll文件所在的路径添加到系统环境java.library.path 中
添加过程需要使用到反射机制来进行,不能使用System.setProperty()进行设置,设置了也无效。直接看代码:
private void addDirToPath(String s){ try { //获取系统path变量对象 Field field=ClassLoader.class.getDeclaredField("sys_paths"); //设置此变量对象可访问 field.setAccessible(true); //获取此变量对象的值 String[] path=(String[])field.get(null); //创建字符串数组,在原来的数组长度上增加一个,用于存放增加的目录 String[] tem=new String[path.length+1]; //将原来的path变量复制到tem中 System.arraycopy(path,0,tem,0,path.length); //将增加的目录存入新的变量数组中 tem[path.length]=s; //将增加目录后的数组赋给path变量对象 field.set(null,tem); } catch (Exception e) { e.printStackTrace(); } }
第三步 加载dll文件
接下来就可以写上下文初始化的方法了:
public void contextInitialized(ServletContextEvent arg0) { //获取存放dll文件的绝对路径(假设将dll文件放在系统根目录下的WEB-INF文件夹中) String path=arg0.getServletContext().getRealPath("WEB-INF"); //将此目录添加到系统环境变量中 addDirToPath(path); //加载相应的dll文件,注意要将'\'替换为'/' System.load(path.replaceAll("\\\\","/")+"/XXXX.dll"); }
第四步 重启启动应用中间件(Tomcat|Weblogic)
至此就可以在你的java程序中使用dll文件的方法了。
The above is the detailed content of Examples of methods for dynamically loading dll files in JavaWeb projects. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When creating a virtual machine, you will be asked to select a disk type, you can select fixed disk or dynamic disk. What if you choose fixed disks and later realize you need dynamic disks, or vice versa? Good! You can convert one to the other. In this post, we will see how to convert VirtualBox fixed disk to dynamic disk and vice versa. A dynamic disk is a virtual hard disk that initially has a small size and grows in size as you store data in the virtual machine. Dynamic disks are very efficient at saving storage space because they only take up as much host storage space as needed. However, as disk capacity expands, your computer's performance may be slightly affected. Fixed disks and dynamic disks are commonly used in virtual machines

If you want to convert a dynamic disk to a basic disk in Windows 11, you should create a backup first as the process will erase all data in it. Why should you convert dynamic disk to basic disk in Windows 11? According to Microsoft, dynamic disks have been deprecated from Windows and their use is no longer recommended. Additionally, Windows Home Edition does not support dynamic disks, so you will not be able to access these logical drives. If you want to combine more disks into a larger volume, it is recommended to use Basic Disks or Storage Spaces. In this article, we will show you how to convert dynamic disk to basic disk on Windows 11 How to convert dynamic disk to basic disk in Windows 11? In the beginning

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

How to use HTML, CSS and jQuery to create a dynamic image carousel. In website design and development, image carousel is a frequently used function for displaying multiple images or advertising banners. Through the combination of HTML, CSS and jQuery, we can achieve a dynamic image carousel effect, adding vitality and appeal to the website. This article will introduce how to use HTML, CSS and jQuery to create a simple dynamic image carousel, and provide specific code examples. Step 1: Set up HTML junction

Web standards are a set of specifications and guidelines developed by W3C and other related organizations. It includes standardization of HTML, CSS, JavaScript, DOM, Web accessibility and performance optimization. By following these standards, the compatibility of pages can be improved. , accessibility, maintainability and performance. The goal of web standards is to enable web content to be displayed and interacted consistently on different platforms, browsers and devices, providing better user experience and development efficiency.

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

Dynamic SQL is one of the very important functions in the MyBatis framework. It can dynamically splice and process SQL statements according to different conditions to achieve flexible SQL operations. Among them, the selection tag is a key tag in dynamic SQL, which is mainly used to implement conditional selection logic. This article will explore the use of selection tags in MyBatis and provide specific code examples for demonstration. 1. Basic syntax of selection tags There are two main forms of selection tags in MyBatis:

The web is a global wide area network, also known as the World Wide Web, which is an application form of the Internet. The Web is an information system based on hypertext and hypermedia, which allows users to browse and obtain information by jumping between different web pages through hyperlinks. The basis of the Web is the Internet, which uses unified and standardized protocols and languages to enable data exchange and information sharing between different computers.
