无法识别作为外部自定义jar添加到应用程序的.class文件

时间:2016-04-06 07:10:32

标签: java eclipse maven jar

我使用maven为dao和model包创建了一个外部自定义jar文件,我在客户端应用程序中导入该jar文件,我想访问dao方法,但是它给出了java.lang.NoClassDefFoundError: com / test / users / dao / UserDao但.class文件存在于jar文件中。我能够在客户端Web应用程序中看到该特定自定义jar文件的包和方法。 在服务端项目中,我们将把它作为客户端应用程序的jar,maven pom文件具有以下依赖性

groupId-- com.test

的artifactId - 服务

名 - testServiceGjcp

包装 - 罐子

版本 - 1.0.0-BUILD-SNAPSHOT

从上面的依赖属性groupid,aritfact id和version,我们使它在客户端应用程序maven pom.xml文件中可用作为依赖,然后我们在客户端maven依赖中添加生成的服务jar文件并构建客户端应用程序, jar已经创建并且能够在生成的jar文件中看到.class文件。

服务项目结构如下 enter image description here

但是我们仍然得到java.lang.NoClassDefFoundError,即使.class文件及其相应的包以及该特定.class文件的方法都存在。

这里我粘贴了实际错误的完整堆栈跟踪。

11:39:34.466 [http-bio-8080-exec-18] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/SpringSecurity/login]

11:39:34.479 [http-bio-8080-exec-18] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - 查找路径/登录的处理程序方法 11:39:34.481 [http-bio-8080-exec-18] DEBUG oswsmmaRequestMappingHandlerMapping - 返回处理程序方法[public org.springframework.web.servlet.ModelAndView com.test.controller.MainController.login(java.lang.String, java.lang.String中,javax.servlet.http.HttpServletRequest)] 11:39:34.481 [http-bio-8080-exec-18] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 返回单例bean的缓存实例' mainController' 11:39:34.481 [http-bio-8080-exec-18] DEBUG o.s.web.servlet.DispatcherServlet - [/ SpringSecurity / login]的Last-Modified值为:-1 11:39:34.543 [http-bio-8080-exec-18] DEBUG o.s.web.servlet.DispatcherServlet - 无法完成请求 org.springframework.web.util.NestedServletException:处理程序处理失败;嵌套异常是java.lang.NoClassDefFoundError:com / test / users / dao / UserDao     在org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1259)〜[spring-webmvc-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)〜[spring-webmvc-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)〜[spring-webmvc-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)[spring-webmvc-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)[spring-webmvc-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在javax.servlet.http.HttpServlet.service(HttpServlet.java:621)[servlet-api.jar:na]     在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)[spring-webmvc-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)[servlet-api.jar:na]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)[catalina.jar:7.0.27]     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)[catalina.jar:7.0.27]     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)[catalina.jar:7.0.27]     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)[catalina.jar:7.0.27]     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)[catalina.jar:7.0.27]     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)[catalina.jar:7.0.27]     在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)[catalina.jar:7.0.27]     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)[catalina.jar:7.0.27]     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)[catalina.jar:7.0.27]     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)[catalina.jar:7.0.27]     在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)[tomcat-coyote.jar:7.0.27]     at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:565)[tomcat-coyote.jar:7.0.27]     at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:307)[tomcat-coyote.jar:7.0.27]     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[na:1.7.0_75]     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)[na:1.7.0_75]     在java.lang.Thread.run(Thread.java:745)[na:1.7.0_75] 引起:java.lang.NoClassDefFoundError:com / test / users / dao / UserDao     在com.test.users.service.MyUserDetailsS​​ervice.loadUserByUsername(MyUserDetailsS​​ervice.java:31)〜[MyUserDetailsS​​ervice.class:na]     在com.test.controller.MainController.login(MainController.java:53)〜[MainController.class:na]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.7.0_75]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)〜[na:1.7.0_75]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.7.0_75]     在java.lang.reflect.Method.invoke(Method.java:606)〜[na:1.7.0_75]     在org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)〜[spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)〜[spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)〜[spring-webmvc-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)〜[spring-webmvc-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)〜[spring-webmvc-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)〜[spring-webmvc-3.2.8.RELEASE.jar:3.2.8.RELEASE]     在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)~ [spring-webmvc-3.2.8.RELEASE.jar:3.2.8.RELEASE]     ...省略了22个常用帧 引起:java.lang.ClassNotFoundException:com.test.users.dao.UserDao     在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)〜[catalina.jar:7.0.27]     在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)〜[catalina.jar:7.0.27]     ...省略了35个常用帧 2016年4月6日上午11:39:34 org.apache.catalina.core.StandardWrapperValve调用 严重:Servlet [appServlet]的Servlet.service()在路径[/ SpringSecurity]的上下文中引发了异常[Handler处理失败;嵌套异常是java.lang.NoClassDefFoundError:com / test / users / dao / UserDao]的根本原因 java.lang.ClassNotFoundException:com.test.users.dao.UserDao     在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)     在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)     at com.test.users.service.MyUserDetailsS​​ervice.loadUserByUsername(MyUserDetailsS​​ervice.java:31)     在com.test.controller.MainController.login(MainController.java:53)     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)     在org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)     在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)     在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)     在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)     在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)     在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)     在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)     在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:621)     在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)     在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)     at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:565)     at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:307)     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)     在java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

实际上你真正的问题是java.lang.NoClassDefFoundError: com/test/users/dao/UserDao at com.test.users.service.MyUserDetailsService.loadUserByUsername(MyUserDetailsService.java:31)

我怀疑是一个ClassLoader问题,例如,如果MyUserDetailsServiceUserDao位于两个不同的网络应用中,它们将位于两个不同的(隔离的)ClassLoader中,这样他们就不会能够见到对方。它们需要位于相同的类加载器(根类加载器或Web应用程序类加载器)中,或者UserDao可以位于根类加载器(webapp类加载器的父级calssloader)中,而MyUserDetailsS​​ervice位于Web应用程序类加载器中