解决java.lang.NoClassDefFoundError: Could not initialize class方案
解决java.lang.NoClassDefFoundError: Could not initialize class方案问题描述:昨天上午来了,同事反应有个页面数据显示为空,最终在上午10点的这个节点查找两个相关微服务的日志,最终定位在assetserver微服务上,下面贴出的是服务器上的错误日志:2018-06-12 10:03:34 [http-nio-8107-exec-3] ERROR
·
解决java.lang.NoClassDefFoundError: Could not initialize class方案
问题描述:
昨天上午来了,同事反应有个页面数据显示为空,最终在上午10点的这个节点查找两个相关微服务的日志,最终定位在assetserver微服务上,下面贴出的是服务器上的错误日志:
2018-06-12 10:03:34 [http-nio-8107-exec-3] ERROR
com.huajin.assetserver.advice.ExpectionAdviceController - Handle request fail!
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.huajin.baymax.logger.Xlogger
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:978)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:108)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.huajin.baymax.logger.Xlogger
at com.huajin.baymax.http.HttpClientInvoker.doPost(HttpClientInvoker.java:117)
at com.huajin.baymax.http.HttpClientUtil.post(HttpClientUtil.java:100)
at com.huajin.baymax.http.HttpClientUtil.post(HttpClientUtil.java:58)
at com.huajin.assetserver.service.api.TradeServerApi.getProjectPeriodByIds(TradeServerApi.java:29)
at com.huajin.assetserver.service.asset.AssetRootService.findAssetBalPeriodList(AssetRootService.java:66)
at com.huajin.assetserver.controller.AssetController.findAssetBalPeriodList(AssetController.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
... 55 common frames omitted
问题分析:
从上述出错的代码位置找到assetserver微服务的代码出错位置,发现是这个微服务使用HTTP的POST请求访问tradeserver微服务出错了,仔细看代码发现是这个项目引用jar包中的
com.huajin.baymax.logger.Xlogger初始化异常,再去看这个Xlogger的代码,这个Xlogger是使用apache开源的log4j日志封装的,
那么初始化Xlogger必先加载org.apache.logging.log4j.Logger和org.apache.logging.log4j.LogManager两个类,那么在初始化这两个类的时候需要加载classpath下的log4j的日志配置文件,而这个配置文件没有提供,因此导致初始化异常出现,那么最终也会导致
Xlogger初始化失败。
这个问题微服务改造为SpringCloud时出现的,但是一直没有出现问题,到今天才出现,而改造好的微服务是使用logbcak日志,因此classpath下提供的是bogbcak的日志配置文件。
解决思路:
思路一:提供log4j需要的日志配置文件,那么可以正常使用,但是微服务的日志logback不再使用
思路二:看了改造微服务的流程,发现有jar包是重新封装了
Xlogger类,使用的日志为logbcak日志,那么可以将旧的jar包替换为改造后的新的jar包,为了统一使用logback日志,所以采用了第二种思路
解决方案:
采用第二种解决思路后,需要将微服务中所有使用的这个旧的jar包的引用进行替换为新的jar包引用,包含配置文件和一些不规范的引用,改造好重新部署运行即可。
总结:
之前也看过这方面的异常报错解决方法,但是自己依然没有找到问题所在,自己确定了classpath下确实有这个类,但是没有想到初始化这个
Xlogger类的时候去加载和初始化它引用的相关日志类,如果相关的类加载合并初始化失败,那么这个Xlogger类的初始化也会失败,没因此会抛出这个异常。
更多推荐
已为社区贡献4条内容
所有评论(0)