【问题标题】:TransformerFactory not found in web app in tomcat root在 tomcat 根目录的 Web 应用程序中找不到 TransformerFactory
【发布时间】:2015-01-28 14:12:13
【问题描述】:

我创建了一个使用 Saxon 库进行 XSLT 转换的 Web 应用程序。 当 Web 应用程序使用自己的名称(等于 webapps 目录中的子目录的名称)部署在 Tomcat 上时,此应用程序运行良好。 但现在我已更改 server.xml 以将此 Web 应用程序用作根应用程序,现在它无法加载 Saxon 库。

我认为这与类加载器有关。我使用类加载器来查找配置文件,当我将此应用程序设置为 tomcat 根应用程序时,这些配置文件也崩溃了。我能够使用 ServletContext 加载配置文件,这样问题就解决了。

但现在我收到以下错误:

Caused by: javax.xml.transform.TransformerFactoryConfigurationError: Provider net.sf.saxon.TransformerFactoryImpl not found

调用的是:

TransformerFactory tf = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",null);

我对@9​​87654324@ 所做的更改(我添加了<Context> 元素):

  <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">

    <!-- SingleSignOn valve, share authentication between web applications
         Documentation at: /docs/config/valve.html -->
    <!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    -->

    <!-- Access log processes all example.
         Documentation at: /docs/config/valve.html
         Note: The pattern used is equivalent to using pattern="common" -->
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log." suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    <Context docBase="foodvoc-pipelines" path="/" reloadable="true"/> 
  </Host>

当使用newInstance 方法中的类名创建TransformerFactory 时,似乎tomcat 找不到SAXON 库。

我可以以编程方式将目录(Web 应用程序中的 lib 目录)添加到类路径吗?或者我应该将 SAXON 库添加到tomcat 在其类路径中使用的另一个目录(哪个?)?我可以直接创建 Saxon TransformerFactory(没有类加载器)吗?还是我让这个应用程序成为根应用程序的配置错误?

【问题讨论】:

  • SAXON 库最初位于何处?在您的 Web 应用程序的 web-inf/lib 中?
  • 是的,在 webapps/foodvoc-pipelines/WEB-INF/lib/ 以及它能够找到的一堆其他库。

标签: java tomcat classloader


【解决方案1】:

我能够通过以编程方式将 tomcat webapp 中的 lib 目录添加到类加载器来完成此工作。

说实话,我对这个解决方案并不满意,但它确实有效......

必须按照answer to this question 中的说明静态添加路径。这是我的FoodVocApplication 类中的静态方法。

private static void addPath(File file) throws NoSuchMethodException, MalformedURLException, InvocationTargetException, IllegalAccessException {
    URI u = file.toURI();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Class<URLClassLoader> urlclass = URLClassLoader.class;
    Method method = urlclass.getDeclaredMethod("addURL", new Class[]{URL.class});
    method.setAccessible(true);
    method.invoke(classLoader, new Object[]{u.toURL()});
}

然后可以通过以下方式在初始化期间调用此方法:

FoodVocApplication.addPath(new File(webInff, "lib"));

webInff 是一个 File 对象,指向 webapp 中的 WEB-INF 目录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-08
    • 2021-10-09
    • 2019-05-01
    • 2019-02-12
    • 1970-01-01
    • 2014-09-22
    • 2017-05-01
    • 2023-03-04
    相关资源
    最近更新 更多