为基本的MVC应用程序进行试运行。获取MovieDAO类的NoClassDefFoundError。
MovieDAO位于:com.library.model servlet MovieController位于:com.library.web
Servlet编译成功,正确导入MovieDAO类。
在Tomcat 8.0 / webapps / Planner / WEB-INF / classes中重现了目录结构的EXACT副本(Planner是项目根文件夹)。在Web服务器上运行测试期间获取NoClassDefFoundError。
Servlet片段:
package com.library.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import com.library.model.*;
import com.library.model.beans.*;
public class MovieController extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String selectedMovie = request.getParameter("title");
MovieDAO movieDAO = new MovieDAO();
XML:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>Movie Index Page</servlet-name>
<servlet-class>com.library.web.MovieController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Movie Index Page</servlet-name>
<url-pattern>/getPlanner</url-pattern>
</servlet-mapping>
MovieDAO片段:
package com.library.model;
import java.util.*;
import java.sql.*;
import com.library.model.beans.*;
public class MovieDAO {
private static final String DB_URL =
"jdbc:mysql://localhost/planner";
// Database credentials
private static final String USER = "Sudipto";
private static final String PASS = "sudi85";
public MovieDetails getMovieDetails(String inputMov) throws SQLException {
MovieDetails movieDetails = new MovieDetails();
try {
//Open a connection
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
完整Stacktrace:
6-Jun-2015 10:58:50.665 SEVERE [http-nio-8080-exec-2] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Movie Index Page] in context with path [/Planner] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoClassDefFoundError: com/library/model/MovieDAO (wrong name: MovieDAO)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2472)
at org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:854)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1274)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
at com.library.web.MovieController.doGet(MovieController.java:16)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
显然是目录/包问题的错位。谁能指出究竟出了什么问题?
答案 0 :(得分:0)
您的MovieDAO
类必须部署到应用程序WEB-INF/classes
文件夹或WEB-INF/lib
的jar中。它可能会遗漏,导致您NoClassDefFoundError
。
答案 1 :(得分:0)
java.lang.NoClassDefFoundError: com/library/model/MovieDAO (wrong name: MovieDAO)
分两部分打破这个错误
java.lang.NoClassDefFoundError: com/library/model/MovieDAO
这表示您的MovieController
正在尝试搜索具有包声明MovieDAO
package com.library.model
(wrong name: MovieDAO)
:基本上,此名称是找到的类的完全限定类名。这告诉它找到了类MovieDAO
但没有任何包声明。
因此,您可以尝试重新编译MovieDAO
类并将其放在类下的正确目录中。还要搜索classes文件夹,看看你是否已经错误地将它放在其他目录中,如果找到则将其删除