启动后出现大量 InvocationTargetException tomcat

A lot of InvocationTargetException after starting tomcat

当我开始 tomcat 我的应用程序时,我看到了很多:

02-Aug-2021 16:49:24.011 WARNING [main] org.apache.tomcat.util.scan.StandardJarScanner.processURLs Failed to scan [file:/opt/tomcat/apache-tomcat-9.0.50/lib/jakarta.annotation-api.jar] from classloader >
        java.io.IOException: java.lang.reflect.InvocationTargetException
                at org.apache.tomcat.util.compat.Jre9Compat.jarFileNewInstance(Jre9Compat.java:209)
Caused by: java.lang.reflect.InvocationTargetException
                at java.base/jdk.internal.reflect.GeneratedConstructorAccessor7.newInstance(Unknown Source)
                at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
                at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
                at org.apache.tomcat.util.compat.Jre9Compat.jarFileNewInstance(Jre9Compat.java:206)
                ... 51 more
        Caused by: java.nio.file.NoSuchFileException: /opt/tomcat/apache-tomcat-9.0.50/lib/jakarta.annotation-api.jar
                at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)

WARNING: Failed to scan [file:/opt/tomcat/apache-tomcat-9.0.50/lib/woodstox-core.jar] from classloader hierarchy
java.io.IOException: java.lang.reflect.InvocationTargetException
Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.GeneratedConstructorAccessor7.newInstance(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
        at org.apache.tomcat.util.compat.Jre9Compat.jarFileNewInstance(Jre9Compat.java:206)
        ... 49 more
Caused by: java.nio.file.NoSuchFileException: /opt/tomcat/apache-tomcat-9.0.50/lib/woodstox-core.jar
        at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)

还有很多很多。在 /opt/tomcat/apache-tomcat-9.0.50/lib 中我有罐子,但 jakarta.annotation-api.jar 中有 -rwxr-x--- 1 root root 25K sie 2 16:49 jakarta.annotation-api-1.3.5.jar。我认为这就是问题所在 - 它正在寻找没有版本的罐子,但我不知道为什么。如何强制 tomcat 取适当的罐子?

检查 catalina.properties 中的 *.loader 属性,看看您是否没有对这些 URL 进行硬编码。默认情况下只有 common.loader 属性 设置为非空值:

common.loader="${catalina.base}/lib","${catalina.base}/lib/*.jar","${catalina.home}/lib","${catalina.home}/lib/*.jar"

这些消息的另一个来源可能是清单扫描功能(参见 source code),它还会扫描另一个 JAR 文件清单中提到的同一文件夹中的 JAR 文件。检查是否是这种情况 运行:

for jar in /opt/tomcat/apache-tomcat-9.0.50/lib/*.jar; do
    unzip -p "$jar" META-INF/MANIFEST.MF | grep '^Class-Path';
done

要解决此问题,您可以通过添加到 $CATALINA_BASE/conf/context.xml:

来完全禁用清单扫描
<Context>
  ...
  <JarScanner scanManifest="false"/>
  ...
</Context>

(比照Tomcat's documentation

备注:除非您运行宁Tomcat为root,否则您的权限将阻止任何其他用户读取这些JAR 文件。您至少应该为用户 运行ning Tomcat.

添加读取 (r) 权限