L-xl 2019-11-16 14:45 采纳率: 75%
浏览 2019
已采纳

将一个简单的数据库查询部署到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的小白,各位大佬见笑了.
  • 写回答

2条回答 默认 最新

  • zqbnqsdsmd 2019-11-16 14:55
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已采纳回答 12月12日

悬赏问题

  • ¥15 怎么用protues测量通频带
  • ¥15 zepelin使用sparkInterpreter 异常
  • ¥15 paho mqtt 接收不到消息
  • ¥15 函数r关于两个分量y,z方向上的图像,分开画r随y的图像,r随z的图像
  • ¥15 如何用Matlab求Pearcey函数的数值积分?
  • ¥15 一个简单的函数定义问题,输出结果是相反的,怎么回事呢?
  • ¥15 页面加载报错,不知道怎么处理
  • ¥20 vue-draggable-resizable选中状态保持
  • ¥15 flink cdc读取SqlServer2016
  • ¥15 毕设是基于stm32的直流电机控制器 能控制电机正反转和加减速 我这个原理图都有什么问题