场景:
javassist是动态代理用于生成字节码的一个开源组件,在web应用中与tomcat和weblogic等中间件集成的时候,遇到了这样的报错(报错在下面贴出)。
报错原因:
之所以报这个错,是因为web应用的classLoader与javassist的classloader不是同一个,那为什么不是同一个classLoader呢?一般的web应用部署到中间件时,都会把jar包放到\WEB-INF\lib的目录下,与WEB-INF\classes目录下的类文件是同一个classloader,但是我们公司在部署web应用的时候不是把jar包放到\WEB-INF\lib下,而是放到一个公共的地方。另外需要注意公共的lib目录和\WEB-INF\lib目录的classpath加载顺序,不要在两个目录都方javassist的jar包,否则也会出现同样的问题。
报错信息:
java.lang.RuntimeException: [source error] nosuch class: com.paic.pafademo.appdemo.web.dubbo.PafaACService
atcom.alibaba.dubbo.common.bytecode.ClassGenerator.toClass(ClassGenerator.java:350)
at com.alibaba.dubbo.common.bytecode.Wrapper.makeWrapper(Wrapper.java:346)
at com.alibaba.dubbo.common.bytecode.Wrapper.getWrapper(Wrapper.java:89)
atcom.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:426)
at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:281)
Truncated. see log file for complete stacktrace
Caused By:javassist.CannotCompileException: [source error] no such class:com.paic.pafademo.appdemo.web.dubbo.PafaACService
at javassist.CtNewMethod.make(CtNewMethod.java:79)
at javassist.CtNewMethod.make(CtNewMethod.java:45)
atcom.alibaba.dubbo.common.bytecode.ClassGenerator.toClass(ClassGenerator.java:318)
at com.alibaba.dubbo.common.bytecode.Wrapper.makeWrapper(Wrapper.java:346)
at com.alibaba.dubbo.common.bytecode.Wrapper.getWrapper(Wrapper.java:89)
Truncated. see log file for complete stacktrace
Caused By: compile error: no such class:com.paic.pafademo.appdemo.web.dubbo.PafaACService
at javassist.compiler.MemberResolver.searchImports(MemberResolver.java:448)
at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:413)
atjavassist.compiler.MemberResolver.lookupClassByJvmName(MemberResolver.java:331)
at javassist.compiler.MemberResolver.resolveJvmClassName(MemberResolver.java:492)
at javassist.compiler.MemberCodeGen.resolveClassName(MemberCodeGen.java:1147)
Truncated. see log file for complete stacktrace
>
<2013-4-1 上午11时13分30秒 CST> weblogic.application.ModuleException:[HTTP:101216]Servlet: "dispatcher" failed to preload on startup inWeb application: "/appdemo". java.lang.RuntimeException: [source error]no such class: com.paic.pafademo.appdemo.web.dubbo.PafaACService atcom.alibaba.dubbo.common.bytecode.ClassGenerator.toClass(ClassGenerator.java:350) at com.alibaba.dubbo.common.bytecode.Wrapper.makeWrapper(Wrapper.java:346) at com.alibaba.dubbo.common.bytecode.Wrapper.getWrapper(Wrapper.java:89) atcom.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:426) at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:281) at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:242) at com.alibaba.dubbo.config.ServiceConfig.export(ServiceConfig.java:143) at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:109) atorg.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97) atorg.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324) atorg.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:929) atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467) atorg.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) atorg.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) atcom.paic.pafa.web.PafaDispatcherServlet.createWebApplicationContext(PafaDispatcherServlet.java:25) atorg.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) atorg.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) at javax.servlet.GenericServlet.init(GenericServlet.java:241) atweblogic.servlet.internal.StubSecurityHelper$ServletInitAction.run(StubSecurityHelper.java:283) at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121) atweblogic.servlet.internal.StubSecurityHelper.createServlet(StubSecurityHelper.java:64) at weblogic.servlet.internal.StubLifecycleHelper.createOneInstance(StubLifecycleHelper.java:58) atweblogic.servlet.internal.StubLifecycleHelper. at weblogic.servlet.internal.ServletStubImpl.prepareServlet(ServletStubImpl.java:531) atweblogic.servlet.internal.WebAppServletContext.preloadServlet(WebAppServletContext.java:1915) atweblogic.servlet.internal.WebAppServletContext.loadServletsOnStartup(WebAppServletContext.java:1889) at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:1807) atweblogic.servlet.internal.WebAppServletContext.start(WebAppServletContext.java:3045) at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1397) at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:460) atweblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:425) atweblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:83) atweblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:119) at weblogic.application.internal.flow.ScopedModuleDriver.start(ScopedModuleDriver.java:200) atweblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:247) atweblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:425) atweblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:83) atweblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:119) at weblogic.application.internal.flow.StartModulesFlow.activate(StartModulesFlow.java:27) atweblogic.application.internal.BaseDeployment$2.next(BaseDeployment.java:1267) atweblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:83) at weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:409) at weblogic.application.internal.EarDeployment.activate(EarDeployment.java:54) atweblogic.application.internal.DeploymentStateChecker.activate(DeploymentStateChecker.java:161) atweblogic.deploy.internal.targetserver.AppContainerInvoker.activate(AppContainerInvoker.java:79) atweblogic.deploy.internal.targetserver.BasicDeployment.activate(BasicDeployment.java:184) at weblogic.deploy.internal.targetserver.BasicDeployment.activateFromServerLifecycle(BasicDeployment.java:361) atweblogic.management.deploy.internal.DeploymentAdapter$1.doActivate(DeploymentAdapter.java:51) at weblogic.management.deploy.internal.DeploymentAdapter.activate(DeploymentAdapter.java:196) atweblogic.management.deploy.internal.AppTransition$2.transitionApp(AppTransition.java:30) atweblogic.management.deploy.internal.ConfiguredDeployments.transitionApps(ConfiguredDeployments.java:233) at weblogic.management.deploy.internal.ConfiguredDeployments.activate(ConfiguredDeployments.java:169) atweblogic.management.deploy.internal.ConfiguredDeployments.deploy(ConfiguredDeployments.java:123) at weblogic.management.deploy.internal.DeploymentServerService.resume(DeploymentServerService.java:173) atweblogic.management.deploy.internal.DeploymentServerService.start(DeploymentServerService.java:89) at weblogic.t3.srvr.SubsystemRequest.run(SubsystemRequest.java:64) at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201) at weblogic.work.ExecuteThread.run(ExecuteThread.java:173) Caused by:javassist.CannotCompileException: [source error] no such class:com.paic.pafademo.appdemo.web.dubbo.PafaACService at javassist.CtNewMethod.make(CtNewMethod.java:79) at javassist.CtNewMethod.make(CtNewMethod.java:45) atcom.alibaba.dubbo.common.bytecode.ClassGenerator.toClass(ClassGenerator.java:318) ... 60 more Caused by: compile error: no such class:com.paic.pafademo.appdemo.web.dubbo.PafaACService at javassist.compiler.MemberResolver.searchImports(MemberResolver.java:448) at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:413) at javassist.compiler.MemberResolver.lookupClassByJvmName(MemberResolver.java:331) atjavassist.compiler.MemberResolver.resolveJvmClassName(MemberResolver.java:492) at javassist.compiler.MemberCodeGen.resolveClassName(MemberCodeGen.java:1147) at javassist.compiler.CodeGen.atDeclarator(CodeGen.java:712) at javassist.compiler.ast.Declarator.accept(Declarator.java:100) at javassist.compiler.CodeGen.atStmnt(CodeGen.java:351) at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50) at javassist.compiler.CodeGen.atStmnt(CodeGen.java:351) at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50) at javassist.compiler.CodeGen.atMethodBody(CodeGen.java:292) at javassist.compiler.CodeGen.atMethodDecl(CodeGen.java:274) at javassist.compiler.ast.MethodDecl.accept(MethodDecl.java:44) at javassist.compiler.Javac.compileMethod(Javac.java:169) at javassist.compiler.Javac.compile(Javac.java:95) at javassist.CtNewMethod.make(CtNewMethod.java:74) ... 62 more at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1399) at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:460) at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:425) atweblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:83) atweblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:119) Truncated. see log file for complete stacktrace Caused By: compile error: no such class:com.paic.pafademo.appdemo.web.dubbo.PafaACService at javassist.compiler.MemberResolver.searchImports(MemberResolver.java:448) at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:413) atjavassist.compiler.MemberResolver.lookupClassByJvmName(MemberResolver.java:331) atjavassist.compiler.MemberResolver.resolveJvmClassName(MemberResolver.java:492) at javassist.compiler.MemberCodeGen.resolveClassName(MemberCodeGen.java:1147) Truncated. see log file for complete stacktrace