Tomcat 7中的ClassNotFoundException

时间:2015-01-22 19:25:03

标签: java tomcat intellij-idea morphia

我在使用Tomcat Web应用程序中的依赖项时遇到问题。我试过了:

  1. 将.jar添加到WEB-INF / lib
  2. 在$ CATALINA_HOME / conf / catalina.properties
  3. 中添加路径
  4. 在IntelliJ
  5. 中添加我的Tomcat配置中的.jar
  6. 将.jar添加到$ TOMCAT_HOME / lib
  7. 但是,在调用依赖项时出现相同的错误:

    java.lang.ClassNotFoundException: org.mongodb.morphia.Morphia
        java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        java.security.AccessController.doPrivileged(Native Method)
        java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        com.turn.quest.mongo.MongoManager.<init>(MongoManager.java:31)
        com.turn.quest.mongo.MongoManager.<clinit>(MongoManager.java:22)
        com.turn.quest.mongo.data.Document.<init>(Document.java:30)
        com.turn.quest.mongo.data.Template.<init>(Template.java:21)
        com.turn.quest.mongo.data.GeoTargetingTemplate.<init>(GeoTargetingTemplate.java:15)
        com.turn.jaxrs.template.TemplateController.get(TemplateController.java:30)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        java.lang.reflect.Method.invoke(Method.java:606)
        com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:168)
        com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:71)
        com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:280)
        com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
        com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
        com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
        com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
        com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1341)
        com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1273)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1223)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1213)
        com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
        org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
        org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
        org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
        com.turn.quest.filters.ClickjackFilter.doFilter(ClickjackFilter.java:27)
        com.turn.quest.filters.PbAuthenticationFilter.doFilter(PbAuthenticationFilter.java:286)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        java.lang.reflect.Method.invoke(Method.java:606)
        org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
        org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
        org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
        org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
        org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
        org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
        org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
        com.sun.proxy.$Proxy315.doFilter(Unknown Source)
        org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
        org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
        org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter.doFilter(AbstractPreAuthenticatedProcessingFilter.java:94)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
        org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
        org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
        org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
        org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
    

    如果我从外部Tomcat调用依赖项,一切正常。

3 个答案:

答案 0 :(得分:1)

上述错误意味着jar包含类org.mongodb.morphia.Morphia在类路径中不存在。因此,请确保在您的Web应用程序的lib或tomcat的lib中包含包含上述类的jar。我想你可以从here获得jar。

答案 1 :(得分:1)

您只需将其添加为IDEA项目定义的依赖项。如果您使用的是maven或gradle,请将依赖项添加到构建文件中。例如,对于maven,您可以将以下内容添加到pom.xml文件中:

<dependency>
    <groupId>org.mongodb.morphia</groupId>
    <artifactId>morphia</artifactId>
    <version>0.109</version>
</dependency>

如果您通过IDEA配置项目:

  1. 转到File > Project Structure
  2. 点击左侧项目设置部分中的模块链接。
  3. 选择使用依赖关系的模块。
  4. 单击依赖关系选项卡。
  5. 点击右侧的添加按钮enter image description here,然后选择&#34;图书馆&#34;
  6. 在&#34;选择图书馆&#34;对话框,单击&#34;新库...&#34;按钮并从弹出列表中选择Java
  7. 在File Browswer对话框中,选择库的JAR文件,或包含JAR文件的目录(它将在目录中添加所有JAR)或包含类的目录。
  8. 在文件选择器对话框中单击“确定”
  9. 在&#34;配置库&#34;对话框,为库提供适当的名称。如果需要,可以将源和javadoc添加到定义中。 (您也可以通过选择多个JAR在File Chooser表单中执行此操作.IDEA会自动确定它们是类,源还是Javadoc。)
  10. 在&#34;配置库&#34;上单击确定。对话框。
  11. 回到&#34;选择图书馆&#34;对话框,应该选择刚刚创建的库。 (如果没有,请选择它。)单击&#34;添加所选项&#34;按钮
  12. 在“项目结构”对话框中单击“确定”。
  13. 有关详细信息,请参阅&#34;配置模块依赖关系和库&#34;在“帮助指南”中(也在web here上)。

    现在,当您通过IDEA Tomcat运行/调试配置将Web应用程序部署到Tomcat时,该库将与其余代码一起部署。请参阅&#34;运行/调试配置:Tomcat&#34; IDEA帮助指南(或web here)中的主题以获取更多信息。

答案 2 :(得分:0)

https://stackoverflow.com/a/42824675/2538853

这是因为Morphia jar不属于你的输出war / jar。 Eclipse或本地构建将它们作为类路径的一部分包含在内,但远程构建或自动/预定构建不会将它们视为类路径的一部分。

您可以使用插件包含相关的jar。

将以下代码段添加到您的pom的插件部分

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>