NoClassDefFoundError动态Web项目Eclipse

时间:2015-07-24 11:05:37

标签: java eclipse maven servlets

我有一个Java Web项目,它在.jar文件中作为外部库通过Build Path -> Configure Build Path -> Add external jars添加。这个jar是使用Maven的clean install参数构建的,它使用org.apache.httpcomponents库作为依赖项添加到他自己的pom.xml文件中。

每当我尝试调用某个url路由来执行该jar调用中的Servlet时,我都会收到此错误java.lang.ClassNotFoundException: my.package.name.MyClass

调用如下所示:

import my.package.name.MyClass;
...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    ...
    MyClass mc = new MyClass();//<- java.lang.ClassNotFoundException
    ...
}

MyClass是一个简单的HTTP客户端,必须向HTTP服务器发送HTTP请求。

这就是我正在做的事:右键点击项目 - &gt;在服务器上运行 - &gt; [显示index.jsp] - &gt;点击索引内的按钮 - &gt;例外

请求的堆栈跟踪:

java.lang.ClassNotFoundException: my.package.name.MyClass
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at my.servlet.package.name.doGet(MyServletClass.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

如果您已使用mvn install构建项目正在使用的jar,请将其添加到pom.xml而不是构建路径。

maven的install部分会将其安装在您的本地存储库中,然后您可以将其包含在您的pom中。

我认为您还可以在构建路径配置的导出选项卡上添加要导出的jar,但我会使用Maven从您的本地仓库导入项目。