ClassNotFoundException oracle.i18n.util.LocaleMapper tomcat TLD 扫描。 ojdbc7 maven dep (xmlparserv2-12.1.0.2.jar transitive) 导致这个错误

ClassNotFoundException oracle.i18n.util.LocaleMapper on tomcat TLD scanning. ojdbc7 maven dep (xmlparserv2-12.1.0.2.jar transitive) causes this error

我在 运行 我的 spring-boot 应用程序嵌入 tomcat 时收到错误消息(尚未尝试使用服务器 tomcat)。

java.lang.IllegalStateException: Tomcat 7 reflection failed
    at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner.scan(SkipPatternJarScanner.java:77)
    at org.apache.catalina.startup.TldConfig.execute(TldConfig.java:271)
    at org.apache.catalina.startup.TldConfig.lifecycleEvent(TldConfig.java:590)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5522)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException: null
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner.scan(SkipPatternJarScanner.java:73)
    ... 12 common frames omitted
Caused by: java.lang.NoClassDefFoundError: oracle/i18n/util/LocaleMapper
    at oracle.xml.parser.v2.XMLReader.setEncoding(XMLReader.java:980)
    at oracle.xml.parser.v2.XMLReader.checkXMLDecl(XMLReader.java:3284)
    at oracle.xml.parser.v2.XMLReader.pushXMLReader(XMLReader.java:570)
    at oracle.xml.parser.v2.XMLReader.pushXMLReader(XMLReader.java:274)
    at oracle.xml.parser.v2.XMLParser.parse(XMLParser.java:232)
    at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1576)
    at org.apache.catalina.startup.TldConfig.tldScanStream(TldConfig.java:565)
    at org.apache.catalina.startup.TldConfig.tldScanJar(TldConfig.java:513)
    at org.apache.catalina.startup.TldConfig.access0(TldConfig.java:61)
    at org.apache.catalina.startup.TldConfig$TldJarScannerCallback.scan(TldConfig.java:300)
    at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:259)
    at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:221)
    ... 17 common frames omitted
Caused by: java.lang.ClassNotFoundException: oracle.i18n.util.LocaleMapper
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 29 common frames omitted

经进一步检查,似乎 ojdbc7 maven dep 所依赖的 orai18n.jar 的先前版本在版本 11.2.0.3 中包含此 class,但更新版本 (12.1.0.2)文件不包含它。

经过一番折腾,我找到了根本原因(编辑:实际上提供的更好的答案更合适 cause/solution,请参阅 Serri 的回答):

根据 this bug comment in spring-boot Oracle SaxParserFactory impl 和(后来 运行 解决第一个问题后进入)DocumentBuilderFactory 实现而不是默认的 xerces impls .使用 META-INF/services/<class-name> 文件更改实现解决了问题:

my.jar中:

每个文件:

// javax.xml.parsers.DocumentBuilderFactory
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl

// javax.xml.parsers.SAXParserFactory
com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl

这会导致每个服务查找器解析标准 java 库中的默认 xerces 实现。

与 xmlparserv2 冲突。 试试这个:

    <dependency>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0.2</version>
        <exclusions>
            <exclusion>
                    <groupId>com.oracle.jdbc</groupId>
                    <artifactId>xmlparserv2</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

在我的例子中,我在 $CATALINA_HOME/lib 下安装了 Oracle Jars,以便能够在 Tomcat 中设置 JNDI DB 连接,只需从 $[= 中删除 xmlparserv2.jar 12=] 目录解决了问题。

xdb 的新更改 com.oracle.jdbc 使用:com.oracle.ojdbc.

<dependency>
     <groupId>com.oracle.ojdbc</groupId>
     <artifactId>xdb</artifactId>
     <version>19.3.0.0</version>
     <exclusions>
          <exclusion>
               <groupId>com.oracle.ojdbc</groupId>
               <artifactId>xmlparserv2</artifactId>
          </exclusion>
     </exclusions>
</dependency>

问题的原因如Serri所说,除了Serri给出的解决方案我认为如果您使用IntelliJ IDEA作为编码工具,请检查Project Structure Artifacts以消除对xmlparserv2的依赖,这对我有用

Remove conflicting dependencies