tomcat运行maven项目Caused by: java.lang.ClassNotFoundException:

问题描述:

期初通过maven创建web项目,通过mvn install运行生成war后再ecplise和myecplise中运行都可以,之后更新过配置之后(不确定更新过什么配置),导致用jetty运行项目可以正常启动,但是在tomcat中运行却一直无法启动,无论是否执行maven install命令,并且出现如下异常:

信息: Starting service Catalina
一月 13, 2017 4:30:21 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/8.0.33
一月 13, 2017 4:30:21 下午 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\conf\Catalina\localhost\java-sm.xml
一月 13, 2017 4:30:21 下午 org.apache.catalina.startup.SetContextPropertiesRule begin
警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:java-sm' did not find a matching property.
一月 13, 2017 4:30:22 下午 org.apache.catalina.core.ContainerBase addChildInternal
严重: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/java-sm]]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
	at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:587)
	at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1798)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	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:745)
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
	at java.lang.Class.getDeclaredFields0(Native Method)
	at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
	at java.lang.Class.getDeclaredFields(Class.java:1916)
	at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106)
	at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:256)
	at org.apache.catalina.startup.WebAnnotationSet.loadApplicationListenerAnnotations(WebAnnotationSet.java:86)
	at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:63)
	at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:335)
	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:782)
	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:306)
	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95)
	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5150)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
	... 10 more
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139)
	... 24 more

一月 13, 2017 4:30:22 下午 org.apache.catalina.startup.HostConfig deployDescriptor
严重: Error deploying configuration descriptor C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\conf\Catalina\localhost\java-sm.xml
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/java-sm]]
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
	at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:587)
	at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1798)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	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:745)

一月 13, 2017 4:30:22 下午 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deployment of configuration descriptor C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\conf\Catalina\localhost\java-sm.xml has finished in 1,001 ms
一月 13, 2017 4:30:22 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\webapps\docs
一月 13, 2017 4:30:22 下午 org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
信息: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [163] milliseconds.
一月 13, 2017 4:30:22 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\webapps\docs has finished in 325 ms
一月 13, 2017 4:30:22 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\webapps\examples
一月 13, 2017 4:30:23 下午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
一月 13, 2017 4:30:23 下午 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
一月 13, 2017 4:30:23 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\webapps\examples has finished in 661 ms
一月 13, 2017 4:30:23 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\webapps\host-manager
一月 13, 2017 4:30:23 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\webapps\host-manager has finished in 55 ms
一月 13, 2017 4:30:23 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\webapps\manager
一月 13, 2017 4:30:23 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\webapps\manager has finished in 55 ms
一月 13, 2017 4:30:23 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\webapps\ROOT
一月 13, 2017 4:30:23 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory C:\yrtz\tools\TOMCAT\apache-tomcat-8.0.33\webapps\ROOT has finished in 31 ms
一月 13, 2017 4:30:23 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
一月 13, 2017 4:30:23 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
一月 13, 2017 4:30:23 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 2252 ms


上述信息是通过ecplise运行获得的异常信息,ecplise部署项目和myecplise部署项目还是有所不同的,ecplise部署的项目通过Server Loactions(双击console中的tomcat即可打开tomcat配置)进行配置,而myecplise是直接将项目的部署到tomcat的webapps下,此处只说明ecplise运行出现的异常问题。我将Server Loactions的配置指定我本地tomcat的安装目录下,那么当通过ecplise部署项目后会在tomcat的目录下与webapps同级生成一个wtpwebapps和backup文件夹,wtpwebapps就是用于存放部署项目的文件夹。

通过分析异常日志Caused by: java.lang.NoClassDefFoundError: 此类异常的原因,一是没有编译、二是缺少jar,应为缺少的类是Spring的jar中文件,因此此处只可能是第二种情况,那么进入到wtpwebapps文件中的项目路径下(我的项目是java-sm),进入wtpwebapps\java-sm\WEB-INF路径下,发现根本就没有lib文件夹,也就是说部署的时候,jar就根本没有部署。


解决办法:

问题原因已经定位到,解决办法就so easy了;解决办法就是需要的lib及jar拷贝到wtpwebapps\java-sm\WEB-INF文件夹下,此种方式有效,但是麻烦;下面通过配置实现自动发布。

右击项目->Properties->Deployment Assembly->add->Java Build Path Entries - >Maven Dependencies Finsh即可。


再次重新部署项目, 发现lib及jar奇迹般的出现了。32个赞。



你可能感兴趣的:(技术经验)