Introduction: Strange and rare java.lang.ClassNotFoundException:ch.qos.lorgback.classic.PatternLayout abnormal error message, when the start, there was an exception, where the problem?
1. Background information
The project is a Web project, using the SLF4J-API, using Logback as the default log package; it worked fine before, after adjusting the log path to Pom.xml, an unexpected error message pops up.
Maven fragments referenced in Pom.xml:
<dependency> <artifactId>slf4j-api</artifactId> <groupid>org.slf4j</groupid > <version>1.7.10</version></dependency><dependency> <groupId> ch.qos.logback</groupid> <artifactId>logback-classic</artifactId> <version> 1.1.3</version> <exclusions><exclusion> <artifactid>slf4j-api</artifactid > <groupId>org.slf4j</groupId></exclusion> </exclusions></dependency >
2. The problem arises
On one day, the following problems suddenly occurred,
17:03:02,893 |-info in c.q.l.core.rolling.defaulttimebasedfilenamingandtriggeringpolicy-setting initial period to Fri APR 17:03:02 CST 201517:03:02,893 |-error in ch.qos.logback.core.joran.action.nestedcomplexpropertyia-could not crea TE component [Layout] of type [ch.qos.lorgback.classic.PatternLayout] java.lang.ClassNotFoundException: Ch.qos.lorgback.classic.PatternLayoutat java.lang.ClassNotFoundException: Ch.qos.lorgback.classic.PatternLayoutat at Org.apache.catalina.loader.WebappClassLoader.loadClass ( webappclassloader.java:1702) at Org.apache.catalina.loader.WebappClassLoader.loadClass (Webappclassloader.java : 1547) at the Ch.qos.logback.core.util.Loader.loadClass (loader.java:125) at Ch.qos.logback.core.joran.action.NestedComplexPropertyIA.begin (nestedcomplexpropertyia.java:100) at Ch.qos.logback.core.joran.spi.Interpreter.callBeginAction (interpreter.java:275) at Ch.qos.logback.core.joran.spi.Interpreter.startElement (interpreter.java:147) at CH.QOS.LOGBACK.CORE.JORAN.SPi. Interpreter.startelement (interpreter.java:129) at Ch.qos.logback.core.joran.spi.EventPlayer.play ( EVENTPLAYER.JAVA:50) at Ch.qos.logback.core.joran.GenericConfigurator.doConfigure (genericconfigurator.java:149 ) at Ch.qos.logback.core.joran.GenericConfigurator.doConfigure (genericconfigurator.java:135) at Ch.qos.logback.core.joran.GenericConfigurator.doConfigure (genericconfigurator.java:99) at Ch.qos.logback.core.joran.GenericConfigurator.doConfigure (genericconfigurator.java:49) at Ch.qos.logback.classic.util.ContextInitializer.configureByResource (contextinitializer.java:77) at Ch.qos.logback.classic.util.ContextInitializer.autoConfig (contextinitializer.java:152) at Org.slf4j.impl.StaticLoggerBinder.init (staticloggerbinder.java:85) at org.slf4j.impl.staticloggerbinder.< Clinit> (staticloggerbinder.java:55) at the Org.slf4j.LoggerFactory.bind (loggerfactory.java:142) at Org.slf4j.LoggerFactory.performInitialization (loggerfactory.java:121) at Org.slf4j.LoggerFactory. Getiloggerfactory (loggerfactory.java:332) at the Org.slf4j.LoggerFactory.getLogger (loggerfactory.java:284) at Org.apache.commons.logging.impl.SLF4JLogFactory.getInstance (slf4jlogfactory.java:156) at Org.apache.commons.logging.impl.SLF4JLogFactory.getInstance (slf4jlogfactory.java:132) at Org.apache.commons.logging.LogFactory.getLog (logfactory.java:274) at Org.springframework.web.context.ContextLoader.initWebApplicationContext (contextloader.java:282) at Org.springframework.web.context.ContextLoaderListener.contextInitialized (contextloaderlistener.java:112) at Org.apache.catalina.core.StandardContext.listenerStart (standardcontext.java:4939) at Org.apache.catalina.core.StandardContext.startInternal (standardcontext.java:5434) at Org.apache.catalina.util.LifecycleBase.start (lifecyclebase.java:150) at Org.apache.catalina.core.ContainerBase $StartChild. Call (containerbase.java:1559) at Org.apache.catalina.core.containerbase$startchild.call ( containerbase.java:1549) at Java.utIl.concurrent.futuretask$sync.innerrun (futuretask.java:303) at Java.util.concurrent.FutureTask.run ( futuretask.java:138) at the Java.util.concurrent.threadpoolexecutor$worker.runtask (threadpoolexecutor.java:886) at At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:908) at Java.lang.Thread.run ( thread.java:662) 17:03:02,893 |-error in [Email protected]:22-no applicable action for [pattern], current ELEMENTPA th is [[configuration][appender][layout][pattern]]17:03:02,903 |-info in Ch.qos.logback.core.rolling.rollingfileappender[thirdpartyappender]-Active log file name:/opt/app/outpay/logs/ es-thirdparty.log.2015-04-24.log17:03:02,903 |-info in ch.qos.logback.core.rolling.rollingfileappender[ Thirdpartyappender]-File property was set to [null]17:03:02,903 |-warn in Ch.qos.logback.core.rolling.rollingfileappender[thirdpartyappender]-Encoder have not been set. Cannot invoke its Init method.
From the log, in the Spring Web context launch process, you need to find the default log framework, the results in the final result, the expected class ch.qos.lorgback.classic.PatternLayout is not found. It is confirmed that this class is actually in the class library of our system, but why is it reported that there are no exceptions found?
3. Analysis and solution of the problem
Why is the error in loading the spring web context, but does the class really exist in the system? The final solution is the order in which the class library is actually inaccessible when the Web context's class library is launched.
Recall that the class libraries loaded in the pom.xml can be limited to different access scopes, meaning that scope restricts the class library packages that the application can access at different times.
The role of scope in the Maven dependency relationship
<scope> is also introduced in the,<dependency> in Pom 4, which primarily manages dependent deployments. Currently <scope> can use 5 values:
* Compile, default, applies to all stages and will be published along with the project.
* provided, similar to compile, expects the JDK, container or user to provide this dependency. such as Servlet.jar.
* Runtime, used only at runtime, such as JDBC driver, for run and test phases.
* Test, used only during testing, to compile and run the test code. Will not be published with the project.
* System, similar to provided, needs to explicitly provide an jar,maven that contains dependencies and does not find it in repository.
At this point, the default value of compile is used in our system. Therefore, you cannot access the available class library Logback.jar when the Web container is started. According to the usual reasoning and analysis, it is applicable to all stages, but why is not available here, can not know.
However, by setting its scope to provided, it is mandatory to specify that it is used by the container, so the current exception problem can be resolved.
The correct settings are:
<!--Logger--><dependency><artifactid>slf4j-api</artifactid><groupid>org.slf4j </groupId><version>1.7.10</version></dependency><dependency><groupId> ch.qos.logback</groupid><artifactid>logback-classic</artifactid><version>1.1.3</ version><exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion></exclusions><scope>provided</ Scope></dependency>
4. Summary
The crux of the problem lies in the correct use of scope scopes.
Weird java.lang.ClassNotFoundException:ch.qos.lorgback.classic.PatternLayout problem solving in Web projects