解决Tomcat 7遇到StackOverflowError的异常

参考网址:http://qingyuexiao.iteye.com/blog/1886059

前言:在写此博客前,首先感谢姚双琪、林瑞丰、网友qingyuexiao的倾囊相助!此博文不过是笔者对于他们给出的方法经过实践成功而做出的归纳。

一、异常描述

使用Tomcat7启动一个项目报错,日志如下:

一月 01, 2016 11:38:08 上午 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:mynewdpi' did not find a matching property.
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server version: Apache Tomcat/7.0.59
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server built: Jan 28 2015 15:51:10 UTC
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server number: 7.0.59.0
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Name: Windows XP
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Version: 5.1
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Architecture: x86
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java Home: C:\Program Files\Java\jdk1.7.0_55\jre
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Version: 1.7.0_55-b13
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Vendor: Oracle Corporation
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_BASE: E:\Spring2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_HOME: E:\Tomcat7\apache-tomcat-7.0.59
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.base=E:\Spring2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.home=E:\Tomcat7\apache-tomcat-7.0.59
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dwtp.deploy=E:\Spring2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.endorsed.dirs=E:\Tomcat7\apache-tomcat-7.0.59\endorsed
一月 01, 2016 11:38:08 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dfile.encoding=GBK
一月 01, 2016 11:38:08 上午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.7.0_55\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_60/bin/client;C:/Program Files/Java/jre1.8.0_60/bin;C:/Program Files/Java/jre1.8.0_60/lib/i386;C:\Program Files\Common Files\NetSarang;D:\MySQL_SOURE\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;D:\oracle\product\10.1.0\db_1\bin;D:\oracle\product\10.1.0\db_1\jre\1.4.2\bin\client;D:\oracle\product\10.1.0\db_1\jre\1.4.2\bin;C:\Program Files\Java\jdk1.7.0_55\bin;E:/apache-maven-3.3.3\bin;D:\STS;;.
一月 01, 2016 11:38:14 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8081"]
一月 01, 2016 11:38:14 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8009"]
一月 01, 2016 11:38:14 上午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 9896 ms
一月 01, 2016 11:38:15 上午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
一月 01, 2016 11:38:15 上午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.59
一月 01, 2016 11:38:18 上午 org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
信息: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [390] milliseconds.
一月 01, 2016 11:38:18 上午 org.apache.catalina.loader.WebappClassLoader validateJarFile
信息: validateJarFile(E:\Spring2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\mynewdpi\WEB-INF\lib\servlet-api.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
一月 01, 2016 11:38:26 上午 org.apache.catalina.core.ContainerBase startInternal
严重: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/mynewdpi]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:188)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:816)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/mynewdpi]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
... 6 more
Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations for web application [/mynewdpi] due to a StackOverflowError. Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies. The class hierarchy being processed was [org.bouncycastle.jce.provider.DSABase->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature->java.security.SignatureSpi->java.security.Signature]
at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:2144)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2090)
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1965)
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1931)
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1916)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1330)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:889)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:386)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5412)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 6 more

一月 01, 2016 11:38:26 上午 org.apache.catalina.core.ContainerBase startInternal
严重: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:188)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:300)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:739)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.startup.Catalina.start(Catalina.java:689)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:321)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:816)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 6 more

一月 01, 2016 11:38:26 上午 org.apache.catalina.startup.Catalina start
严重: The required Server component failed to start so Tomcat is unable to start.
org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.startup.Catalina.start(Catalina.java:689)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:321)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Catalina]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:739)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 9 more
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:300)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 11 more

一月 01, 2016 11:38:26 上午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["http-bio-8081"]
一月 01, 2016 11:38:26 上午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["ajp-bio-8009"]
一月 01, 2016 11:38:26 上午 org.apache.catalina.core.StandardService stopInternal
信息: Stopping service Catalina
一月 01, 2016 11:38:26 上午 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["http-bio-8081"]
一月 01, 2016 11:38:26 上午 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["ajp-bio-8009"]

 

二、分析及解决

第一次碰到这个异常,开始以为是org.apache.catalina.LifecycleException,但后来才发现这不过是JVM抛出的最大的异常,光知道这个没什么用,最后才准确定位到引起此异常的原因(其实日志信息已经很清楚的给出了):Unable to complete the scan for annotations for web application [/mynewdpi] due to a StackOverflowError. Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies.

http://qingyuexiao.iteye.com/blog/1886059 文章中讲述了引起此异常的原因:

tomcat7.0.3X版本支持servlet3.0的特性,比如说支持@WebServlet、@WebListener,要支持这些特性,tomcat就得去扫描所有的jar包里面的每个类。这个异常表明在扫描jar包的时候,递归调用太深,导致栈溢出了,tomcat给了一个馊主意,让你增大xss,这个还是不好,xss加大了,可用线程数就少了。 

分析tomcat源代码,发现它扫描的流程如下: 

1.扫描所有jar包 

2.通过查找jar包中META-INF/services/javax.servlet.ServletContainerInitializer文件内的定义,初始化ServletContainerInitializer实现 

3.如果web.xml中配置了metadata-complete="true" 或者没有找到ServletContainerInitializer实现,都不会继续扫描jar包

 

笔者也采用过以增加Xss(线程内存)的方法解决,但最终失败了(虽然不建议这样做)。那么就采用其他的方式进行解决:

一、在web.xml中配置metadata-complete="true" 。需说明的是:web-app的配置应是3.0版本,如果你的是2.5或者其他版本,可直接复制下面的信息。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0" metadata-complete="true">
    
</web-app>

二、修改tomcat的catalina.properties

如果你不想使用servlet3.0 annotation支持,在tomcat的安装目录的conf目录中找到catalina.properties,在tomcat.util.scan.DefaultJarScanner.jarsToSkip的值后面加一个",*",这样就不会扫描所有的jar包了。启动更快,也不会出异常。 

如下图:

三、修复Eclipse的项目发布地址

参见我的另一篇博客:http://www.cnblogs.com/wql025/p/5084653.html

注:以eclipse的run as 启动项目,eclipse一般的默认方式是将项目部署到项目所在工作空间的metadata,而上面的博客讲解了如何将项目部署到Tomcat的webapp目录,这也是解决此异常不可或缺的一步(当然你可以对上面的方法进行分步尝试,先做第一步,不行了再做第二步...)

 

 后记:修改tomcat的catalina.properties这一步至关重要!而且,Tomcat会出现再启动还原的情况,所以如果启动再遇此异常,再次更改这里即可。

 

 

 

 

posted @ 2016-01-02 23:34  Tom1997  阅读(3733)  评论(0编辑  收藏  举报