将一个简单的数据库查询部署到Tomcat时java.lang.NoClassDefFoundError: Could not initialize class

在完成一个数据库查询操作部署到Tomcat时报错java.lang.NoClassDefFoundError: Could not initialize class.

在没有使用服务器时,查询功能可以正常运行.

在启动服务器查询时报错
Servlet代码如下:

package com.lxl.web.servlet;

import com.lxl.smis.dao.impl.IStudentDAO;
import com.lxl.smis.dao.impl.StudentDAOImpl;
import com.lxl.smis.domain.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student/list")
public class ListStudentServlet extends HttpServlet {
    private IStudentDAO dao;

    public void init() throws ServletException {
       dao = new StudentDAOImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接受请求参数,封装对象
        //2.调用业务方法处理请求
        System.out.println("hello");
        List<Student>  stus= dao.listAll();
        for (Student stu : stus) {
            System.out.println(stu);
        }
        //3.控制界面跳转
    }
}

错误提示:

图片说明

```16-Nov-2019 15:29:27.029 严重 [http-nio-8080-exec-3] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [com.lxl.web.servlet.ListStudentServlet] in context with path [] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoClassDefFoundError: Could not initialize class com.lxl.smis.util.JDBCUtil
at com.lxl.smis.util.JDBCTemplate.query(JDBCTemplate.java:73)
at com.lxl.smis.dao.impl.StudentDAOImpl.listAll(StudentDAOImpl.java:41)
at com.lxl.web.servlet.ListStudentServlet.service(ListStudentServlet.java:28)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)




使用DeBug运行
感觉是这里出现问题,但并不知道到是什么意思
![图片说明](https://img-ask.csdn.net/upload/201911/16/1573886445_32104.png)


查找网上解决方法
说这个报错的问题原因就是static变量的初始化失败导致


但我如果不用Servlet运行这个查找功能,直接在本地实现这个方法,是可以正常运行的.

请教一下各位大佬,是什么原因?

我是初学JAVA的小白,各位大佬见笑了.

1个回答

qq_40060116
L-xl 我没有使用内部类
5 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问