在java Web应用程序中加载外部库 [英] Load external library in java web application
问题描述
我的方案如下:
我有一个部署到servlet容器的WebApp.war。此WebApp.war在WEB-INF / lib中包含以下库:
I have a WebApp.war that is deployed to a servlet container. This WebApp.war contains in WEB-INF/lib the following libraries:
- lib_a.jar
- lib_b.jar
我有另一个库,比如lib_vendor.jar,我无法在WebApp / WEB-INF / lib中部署因为许可问题所以我让我们的客户在安装应用程序后在tomcat / lib中复制这个库。但由于lib_vendor.jar需要在Web应用程序类加载器中加载lib_a.jar和lib_b.jar,因此我无法使用lib_vendor.jar。
I have one other library, say lib_vendor.jar, that I cannot deploy within WebApp/WEB-INF/lib because of licensing issues so I let our customers to copy this library in tomcat/lib after application installation. But since lib_vendor.jar requires lib_a.jar and lib_b.jar that are loaded in the web application class loader, I cannot use lib_vendor.jar.
如何加载外部库(不在WEB-INF / lib中)在Web应用程序的同一个类加载器中?
How can I load an external library (not in WEB-INF/lib) in the same classloader of a web application?
推荐答案
因为您使用的是Tomcat ,您可以利用 VirtualWebappLoader 。
Since you are using Tomcat, you could leverage the VirtualWebappLoader.
添加 META-INF / context.xml
whith
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/somepath/myapp">
<Loader className="org.apache.catalina.loader.VirtualWebappLoader"
virtualClasspath="/somedir/*.jar"/>
</Context>
还要记住virtualClasspath属性必须是绝对路径,如下面的注释中所述。
Remember also that the virtualClasspath attribute must be a absolute path, as correctly stated in the comment below.
这篇关于在java Web应用程序中加载外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!