java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
如果最近你也在学习SpringBoot,当项目运行时,你有可能会遇到这样的异常:java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
一、异常描述:
1 org.springframework.context.ApplicationContextException: Unable to start embedded 2 container; nested exception is java.lang.NoClassDefFoundError: 3 org/apache/juli/logging/LogFactory 4 at 5 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh 6 (EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 7 at org.springframework.context.support.AbstractApplicationContext.refresh 8 (AbstractApplicationContext.java:537) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 9 at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh 10 (EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 11 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) 12 [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 13 at org.springframework.boot.SpringApplication.refreshContext 14 (SpringApplication.java:372) [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 15 at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) 16 [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 17 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1187) 18 [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 19 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1176) 20 [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 21 at com.fhcq.MySpringBootApplication.main(MySpringBootApplication.java:10) 22 [classes/:na] 23 Caused by: java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory 24 at org.apache.catalina.util.LifecycleBase.<clinit>(LifecycleBase.java:41) ~ 25 [catalina.jar:8.5.23] 26 at 27 org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory. 28 getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:167) ~[spring-boot- 29 1.4.7.RELEASE.jar:1.4.7.RELEASE] 30 at 31 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext. 32 createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164) ~[spring-boot- 33 1.4.7.RELEASE.jar:1.4.7.RELEASE] 34 at 35 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh 36 (EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 37 ... 8 common frames omitted 38 Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory 39 at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_144] 40 at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_144] 41 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.8.0_144] 42 at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_144] 43 ... 12 common frames omitted
二、错误分析:
很显然,缺少 tomcat-juli.jar,找不到类 org.apache.juli.logging.LogFactory,而此jar包在Tomcat安装目录的bin目录下。
三、解决办法:
在Maven工程的pom文件中加入 tomcat-juli.jar 的依赖:
1 <dependency> 2 <groupId>org.apache.tomcat</groupId> 3 <artifactId>tomcat-juli</artifactId> 4 <version>7.0.27</version> 5 </dependency>
然而,将 tomcat-juli-7.0.27.jar 版本的依赖加入工程后,运行工程发现Console控制台报出新的异常:
java.lang.NoClassDefFoundError: org/apache/juli/WebappProperties
一、异常描述:
1 org.springframework.context.ApplicationContextException: Unable to start embedded 2 container; nested exception is java.lang.NoClassDefFoundError: 3 org/apache/juli/WebappProperties 4 at 5 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh 6 (EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 7 at org.springframework.context.support.AbstractApplicationContext.refresh 8 (AbstractApplicationContext.java:537) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 9 at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh 10 (EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 11 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) 12 [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 13 at org.springframework.boot.SpringApplication.refreshContext 14 (SpringApplication.java:372) [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 15 at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) 16 [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 17 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1187) 18 [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 19 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1176) 20 [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 21 at com.fhcq.MySpringBootApplication.main(MySpringBootApplication.java:10) 22 [classes/:na] 23 Caused by: java.lang.NoClassDefFoundError: org/apache/juli/WebappProperties 24 at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_144] 25 at java.lang.ClassLoader.defineClass(Unknown Source) ~[na:1.8.0_144] 26 at java.security.SecureClassLoader.defineClass(Unknown Source) ~[na:1.8.0_144] 27 at java.net.URLClassLoader.defineClass(Unknown Source) ~[na:1.8.0_144] 28 at java.net.URLClassLoader.access$100(Unknown Source) ~[na:1.8.0_144] 29 at java.net.URLClassLoader$1.run(Unknown Source) ~[na:1.8.0_144] 30 at java.net.URLClassLoader$1.run(Unknown Source) ~[na:1.8.0_144] 31 at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_144] 32 at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_144] 33 at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_144] 34 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.8.0_144] 35 at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_144] 36 at 37 org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.prep 38 areContext(TomcatEmbeddedServletContainerFactory.java:208) ~[spring-boot- 39 1.4.7.RELEASE.jar:1.4.7.RELEASE] 40 at 41 org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory. 42 getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:176) ~[spring-boot- 43 1.4.7.RELEASE.jar:1.4.7.RELEASE] 44 at 45 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext. 46 createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164) ~[spring-boot- 47 1.4.7.RELEASE.jar:1.4.7.RELEASE] 48 at 49 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh 50 (EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE] 51 ... 8 common frames omitted 52 Caused by: java.lang.ClassNotFoundException: org.apache.juli.WebappProperties 53 at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_144] 54 at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_144] 55 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.8.0_144] 56 at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_144] 57 ... 24 common frames omitted
二、错误分析:
由于我的 Eclipse 绑定的 Server 是 apache-tomcat-8.5.23,而引入的依赖 tomcat-juli-7.0.27.jar 版本过低,找不到类 org.apache.juli.WebappProperties
三、解决办法:
将 tomcat-juli-8.5.2.jar 版本的依赖加入pom文件,运行工程,至此 Console 控制台打印日志正常,问题解决。