Java – slf4j error: class loader has different types of class objects

Try to find out why I received the following error in the Tomcat log:

Caused by: java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class,org/slf4j/LoggerFactory,and the class loader (instance of sun/misc/Launcher$AppClassLoader) for the method's defining class,org/slf4j/impl/StaticLoggerBinder,have different Class objects for the type org/slf4j/ILoggerFactory used in the signature

In my gradle file, I use slf4j version 1.7 7. In tomcat, I have jul-to-slf4j-1.7 7. Jar and slf4j-api-1.7 7. Jar, so I'm confused

This is a stack trace – thank you for your help!

Total time: 2.982 secs
Using CATALINA_BASE:   /usr/local/Cellar/tomcat/8.0.18/libexec
Using CATALINA_HOME:   /usr/local/Cellar/tomcat/8.0.18/libexec
Using CATALINA_TMPDIR: /usr/local/Cellar/tomcat/8.0.18/libexec/temp
Using JRE_HOME:        /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
Using CLASSPATH:       /usr/local/Cellar/tomcat/8.0.18/libexec/bin/jul-to-slf4j-1.7.7.jar:/usr/local/Cellar/tomcat/8.0.18/libexec/bin/slf4j-api-1.7.7.jar:/usr/local/Cellar/tomcat/8.0.18/libexec/bin/logback-classic-1.1.2.jar:/usr/local/Cellar/tomcat/8.0.18/libexec/bin/logback-core-1.1.2.jar:/usr/local/Cellar/tomcat/8.0.18/libexec/bin/logback-config/:/usr/local/Cellar/tomcat/8.0.18/libexec/bin/bootstrap.jar:/usr/local/Cellar/tomcat/8.0.18/libexec/bin/tomcat-juli.jar
19:03:55.979 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - Server version:        Apache Tomcat/8.0.18
19:03:55.981 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - Server built:          Jan 23 2015 11:56:07 UTC
19:03:55.981 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - Server number:         8.0.18.0
19:03:55.982 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - OS Name:               Mac OS X
19:03:55.982 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - OS Version:            10.10
19:03:55.982 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - Architecture:          x86_64
19:03:55.982 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - Java Home:             /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre
19:03:55.982 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - JVM Version:           1.8.0_25-b17
19:03:55.982 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - JVM Vendor:            Oracle Corporation
19:03:55.982 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - CATALINA_BASE:         /usr/local/Cellar/tomcat/8.0.18/libexec
19:03:55.983 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - CATALINA_HOME:         /usr/local/Cellar/tomcat/8.0.18/libexec
19:03:55.983 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - Command line argument: -Djava.util.logging.config.file=/usr/local/Cellar/tomcat/8.0.18/libexec/conf/logging.properties
19:03:55.983 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
19:03:55.983 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - Command line argument: -Djava.endorsed.dirs=/usr/local/Cellar/tomcat/8.0.18/libexec/endorsed
19:03:55.983 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - Command line argument: -Dcatalina.base=/usr/local/Cellar/tomcat/8.0.18/libexec
19:03:55.984 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - Command line argument: -Dcatalina.home=/usr/local/Cellar/tomcat/8.0.18/libexec
19:03:55.984 [main] INFO  org.apache.catalina.startup.VersionLoggerListener - Command line argument: -Djava.io.tmpdir=/usr/local/Cellar/tomcat/8.0.18/libexec/temp
19:03:55.984 [main] INFO  org.apache.catalina.core.AprLifecycleListener - The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/jadekler/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
19:03:56.076 [main] INFO  org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
19:03:56.092 [main] INFO  org.apache.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read
19:03:56.094 [main] INFO  org.apache.coyote.ajp.AjpNioProtocol - Initializing ProtocolHandler ["ajp-nio-8009"]
19:03:56.096 [main] INFO  org.apache.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read
19:03:56.096 [main] INFO  org.apache.catalina.startup.Catalina - Initialization processed in 510 ms
19:03:56.115 [main] INFO  org.apache.catalina.core.StandardService - Starting service Catalina
19:03:56.115 [main] INFO  org.apache.catalina.core.StandardEngine - Starting Servlet Engine: Apache Tomcat/8.0.18
19:03:56.132 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - deploying web application archive /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/websiteskeleton.war
19:03:56.812 [localhost-startStop-1] ERROR org.apache.catalina.core.StandardContext - Error during ServletContainerInitializer processing
javax.servlet.ServletException: Failed to instantiate WebApplicationInitializer class
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:160) ~[spring-web-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5143) ~[catalina.jar:8.0.18]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.0.18]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) [catalina.jar:8.0.18]
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) [catalina.jar:8.0.18]
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714) [catalina.jar:8.0.18]
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:917) [catalina.jar:8.0.18]
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1701) [catalina.jar:8.0.18]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_25]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_25]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_25]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]
Caused by: java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class,have different Class objects for the type org/slf4j/ILoggerFactory used in the signature
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:306) ~[slf4j-api-1.7.7.jar:1.7.7]
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:276) ~[slf4j-api-1.7.7.jar:1.7.7]
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:288) ~[slf4j-api-1.7.7.jar:1.7.7]
    at com.websiteskeleton.core.HelloInitializer.<init>(HelloInitializer.java:13) ~[HelloInitializer.class:na]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_25]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_25]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_25]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408) ~[na:1.8.0_25]
    at java.lang.Class.newInstance(Class.java:438) ~[na:1.8.0_25]
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:157) ~[spring-web-4.1.0.RELEASE.jar:4.1.0.RELEASE]
    ... 12 common frames omitted
19:03:56.821 [localhost-startStop-1] ERROR org.apache.catalina.core.StandardContext - Context [/websiteskeleton] startup Failed due to prevIoUs errors
19:03:56.825 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - Deployment of web application archive /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/websiteskeleton.war has finished in 693 ms
19:03:56.826 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - deploying web application directory /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/docs
19:03:56.893 [localhost-startStop-1] INFO  org.apache.jasper.servlet.TldScanner - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
19:03:56.904 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - Deployment of web application directory /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/docs has finished in 78 ms
19:03:56.904 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - deploying web application directory /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/examples
19:03:57.133 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - Deployment of web application directory /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/examples has finished in 228 ms
19:03:57.133 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - deploying web application directory /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/host-manager
19:03:57.192 [localhost-startStop-1] INFO  org.apache.jasper.servlet.TldScanner - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
19:03:57.195 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - Deployment of web application directory /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/host-manager has finished in 62 ms
19:03:57.195 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - deploying web application directory /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/manager
19:03:57.252 [localhost-startStop-1] INFO  org.apache.jasper.servlet.TldScanner - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
19:03:57.254 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - Deployment of web application directory /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/manager has finished in 59 ms
19:03:57.254 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - deploying web application directory /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/ROOT
19:03:57.307 [localhost-startStop-1] INFO  org.apache.jasper.servlet.TldScanner - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
19:03:57.308 [localhost-startStop-1] INFO  org.apache.catalina.startup.HostConfig - Deployment of web application directory /usr/local/Cellar/tomcat/8.0.18/libexec/webapps/ROOT has finished in 54 ms
19:03:57.310 [main] INFO  org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
19:03:57.314 [main] INFO  org.apache.coyote.ajp.AjpNioProtocol - Starting ProtocolHandler ["ajp-nio-8009"]
19:03:57.315 [main] INFO  org.apache.catalina.startup.Catalina - Server startup in 1218 ms

Solution

The Tomcat log shows another slf4j-1.7. On the classpath 7.jar:

Use classpath: / usr / local / cell / Tomcat / 8.0 18/libexec/bin/jul-to-slf4j-1.7. 7.jar:/usr/local/Cellar/tomcat/8.0. 18/libexec/bin/slf4j -API-1.7. 7.jar:/usr/local/Cellar/tomcat/8.0. 18/libexec/bin/logback-classic-1.1. 2.jar:/usr/local/Cellar/tomcat/8.0. 18/libexec /bin/logback-core-1.1. 2.jar:/usr/local/Cellar/tomcat/8.0. 18/libexec/bin/logback-config/:/usr/local/Cellar/tomcat/8.0. 18 / libexec / bin / bootstrap jar:/usr/local/Cellar/tomcat/8.0. 18/libexec/bin/tomcat-juli. jar

When Tomcat is initialized, it initializes jul-to-slf4j and slf4j API These jars are being loaded by classloader, which loads Tomcat jar (sun / misc / launcher $appclassloader)

When your application tries to access slf4j API classes, it uses the webapplication classloader (ORG / Apache / Catalina / loader / webappclassloader), which searches for class definitions on the app classpath by default before the system classpath Note that this can be changed: https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html

In Java, a class signature consists of the fully qualified name of the class and the class loader that loads the class If a class is loaded twice from two different class loaders (the same hierarchy), the JVM will have two different classes Trying to point an object reference of one class to a variable of another class will display ClassCastException See this blog post (item 5) for more information: https://techblug.wordpress.com/2012/03/17/classloader-tips/

So, as far as I know, you can delete slf4j – *. From the Tomcat classpath Jar to solve this problem, or if you have multiple webapps using the same version of slf4j, you can remove the jar from the webapp, so it will use those loaded by the Tomcat class loader

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>