Tomcat 7遇到StackOverflowError的异常的解决办法

抓虫师

2017-11-07

59

0

之前使用tomcat7时遇到启动报错问题,日志如下:

严重: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/admin]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
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.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: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/admin]]
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 [/admin] 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.asn1.ASN1EncodableVector->org.bouncycastle.asn1.DEREncodableVector->org.bouncycastle.asn1.ASN1EncodableVector]
at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:2188)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2135)
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2010)
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1976)
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1961)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1319)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:376)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 
此处问题已经很明确,是第三方包的API有问题。

从以下方面检查错误:

1. 栈内存不足,修改tomcat启动参数,增加栈内存, 如:-Xss100m。排除

2. 在你的应用中告诉tomcat不要扫描annotations.头中增加 metadata-complete="true" 。排除

3. 限制tomcat扫描jar 文件,参考:http://tomcat.apache.org/tomcat-7.0-doc/config/jar-scanner.html

4.这个可能是第三方api的一个bug.可以到其官网下载最新版本看看是否修复。 问题所在

解决:因<dependency>

<groupId>com.squareup.okhttp</groupId>

<artifactId>mockwebserver</artifactId>

<version>1.5.4</version>

<scope>test</scope>

</dependency> 

maven的二次依赖引入Bouncy Castlejava包,因再试未用测试将取消其注入。如需使用可以重新寻找新的版本看是否解决。或者运用不扫描其jar包

 

使用tomcat 7.0.3x版本的同学可以发现tomcat启动慢了不少,而且还可能遇到如下启动时异常: 

Unable to complete the scan for annotations for web application [] due to a StackOverflowError. Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies. 

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包 

网上大多数的答案的都是说在web.xml中加入了metadata-complete="true"就能避免这个异常。确实在很多场景下,这个异常是能够避免。但是使用spring-web-3.1.0.RELEASE的同学就杯具了,这个jar包中定义了一个ServletContainerInitializer,还是导致了扫描jar包。 

我们可以用另外的办法来解决这个问题,我们让tomcat不扫描指定的jar包,tomcat就要轻松得多了,org.apache.tomcat.util.scan.StandardJarScanner中定义了defaultJarsToSkip,有了这个东东,我们就可以跳过某些jar包。 

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

tomcat.util.scan.DefaultJarScanner.jarsToSkip=\
bootstrap.jar,commons-daemon.jar,tomcat-juli.jar,\
annotations-api.jar,el-api.jar,jsp-api.jar,servlet-api.jar,websocket-api.jar,\
catalina.jar,catalina-ant.jar,catalina-ha.jar,catalina-tribes.jar,\
jasper.jar,jasper-el.jar,ecj-*.jar,\
tomcat-api.jar,tomcat-util.jar,tomcat-coyote.jar,tomcat-dbcp.jar,\
tomcat-jni.jar,tomcat-spdy.jar,\
tomcat-i18n-en.jar,tomcat-i18n-es.jar,tomcat-i18n-fr.jar,tomcat-i18n-ja.jar,\
tomcat-juli-adapters.jar,catalina-jmx-remote.jar,catalina-ws.jar,\
tomcat-jdbc.jar,\
tools.jar,\
commons-beanutils*.jar,commons-codec*.jar,commons-collections*.jar,\
commons-dbcp*.jar,commons-digester*.jar,commons-fileupload*.jar,\
commons-httpclient*.jar,commons-io*.jar,commons-lang*.jar,commons-logging*.jar,\
commons-math*.jar,commons-pool*.jar,\
jstl.jar,taglibs-standard-spec-*.jar,\
geronimo-spec-jaxrpc*.jar,wsdl4j*.jar,\
ant.jar,ant-junit*.jar,aspectj*.jar,jmx.jar,h2*.jar,hibernate*.jar,httpclient*.jar,\
jmx-tools.jar,jta*.jar,log4j.jar,log4j-1*.jar,mail*.jar,slf4j*.jar,\
xercesImpl.jar,xmlParserAPIs.jar,xml-apis.jar,\
junit.jar,junit-*.jar,hamcrest*.jar,org.hamcrest*.jar,ant-launcher.jar,\
cobertura-*.jar,asm-*.jar,dom4j-*.jar,icu4j-*.jar,jaxen-*.jar,jdom-*.jar,\
jetty-*.jar,oro-*.jar,servlet-api-*.jar,tagsoup-*.jar,xmlParserAPIs-*.jar,\
xom-*.jar,*



tomcat在处理扫描是还有个小bug,比如我遇到了 

SEVERE: Unable to process Jar entry [__MACOSX/cn/****/._HandlerFactory.class] from... 

这是tomcat在扫描到以.class为后缀的文件后,就分析类,很明显,此文件都不是java类文件。tomcat不应该只判断后缀为.class就是java类文件。 

发表评论

全部评论:0条

houzhe11

JAVA从业者