javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver

huangapple go评论76阅读模式
英文:

javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver

问题

我正在尝试运行简单的代码与MySQL连接。我正在使用Eclipse和Tomcat。
我已经在Libraries中添加了mysql-connector-java-8.0.1.9.jar:

Libraries-->mysql-->mysql-connector-java-8.0.1.9.jar

我已经花了几个小时来添加和移除mysql-connector-java-8.0.1.9.jar到构建路径,但无法让它运行。请帮忙。我需要在运行配置中添加任何内容吗?

这是Eclipse浏览器上的错误:

    HTTP Status 500 – Internal Server Error
    
    异常报告类型
    
    消息 javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
    
    描述 服务器遇到了一个意外情况,阻止它完成请求。
    
    异常 
    org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
    	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:599)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:500)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    
    根本原因 
    javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
    	org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:917)
    	org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:846)
    	org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:129)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    
    根本原因 
    java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
    	compute.DB.con(DB.java:21)
    	org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    
    根本原因 
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1951)
    	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1794)
    	compute.DB.con(DB.java:21)
    	org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    
    请注意,根本原因的完整堆栈跟踪在服务器日志中可用。

这是控制台输出:

    > Mar 16, 2020 4:57:27 PM org.apache.catalina.core.StandardContext reload
    INFO: 正在重新加载名称为[/Mysql]的上下文
    Mar 16, 2020 4:57:27 PM org.apache.catalina.core.StandardContext reload
    INFO: 重新加载名称为[/Mysql]的上下文已完成
    Mar 16, 2020 4:57:41 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: 在带有路径[/Mysql]的上下文中为servlet[jsp]提供服务时,抛出异常[javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver],根本原因为
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1951)
    	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1794)
    	at compute.DB.con(DB.java:21)
    	at org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
    	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	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:219)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
    	at org.apache.catalina.valves

<details>
<summary>英文:</summary>

I am trying to run simple code to connect with mysql. I m using eclipse and tomcat.
I have added mysql-connector-java-8.0.1.9.jar in Libraries: 

Libraries--&gt;mysql--&gt;mysql-connector-java-8.0.1.9.jar

I have been at it for several hours adding and removing mysql-connector-java-8.0.1.9.jar to build path cannot get it running. Please help. Do I need to add any thing in run configuration??

This is the error on eclipse browser: 

    HTTP Status 500 – Internal Server Error
    
    
    Type Exception Report
    
    Message javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
    
    Description The server encountered an unexpected condition that prevented it from fulfilling the request.
    
    Exception 
    org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
    	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:599)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:500)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    
    
    
    Root Cause 
    javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
    	org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:917)
    	org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:846)
    	org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:129)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    
    
    
    Root Cause 
    java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
    	compute.DB.con(DB.java:21)
    	org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    
    
    
    Root Cause 
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1951)
    	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1794)
    	compute.DB.con(DB.java:21)
    	org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    
    
    
    Note The full stack trace of the root cause is available in the server logs.

This is the output in console: 

    &gt; Mar 16, 2020 4:57:27 PM org.apache.catalina.core.StandardContext reload
    INFO: Reloading Context with name [/Mysql] has started
    Mar 16, 2020 4:57:27 PM org.apache.catalina.core.StandardContext reload
    INFO: Reloading Context with name [/Mysql] is completed
    Mar 16, 2020 4:57:41 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [jsp] in context with path [/Mysql] threw exception [javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver] with root cause
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1951)
    	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1794)
    	at compute.DB.con(DB.java:21)
    	at org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
    	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	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:219)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1195)
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)
    	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	at java.base/java.lang.Thread.run(Thread.java:834)
    
    Try - 05 Trying to establish con in regCheck.jsp
    Con method called Try 5A
    Try 05a - Trying to connect



This is DB.java: 


 

    package compute;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    import java.sql.*;
    
    public class DB {
    	
    	
    	final static String DB_URL = &quot;jdbc:mysql://localhost:3306/new&quot;;
    	final static String USER = &quot;admin&quot;;
    	final static String PASS = &quot;admin&quot;;
    	
    	public static Connection con() {
    		System.out.println(&quot;Con method called Try 5A&quot;);
    		Connection con;
    		try {
    			System.out.println(&quot;Try 05a - Trying to connect&quot;);
    			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    			con = DriverManager.getConnection(DB_URL, USER, PASS);
    			System.out.println(&quot;Try 05a- con established &quot;);
    			return con;
    		}catch(Exception e) {
    			System.out.println(e);
    			return null;
    		}
    	}
    
    	public static Statement st() {
    		Statement stmt;
    		try {
    			stmt=DB.con().createStatement();
    			return null;
    		}catch(Exception e) {
    			System.out.println(e);
    			return null;
    		}
    	}
    }

This is regCheck.jsp: 

    &lt;%@page import=&quot;java.sql.Connection&quot; %&gt;
    &lt;%@page import=&quot;compute.DB&quot; %&gt;
    &lt;%@page import=&quot;java.sql.PreparedStatement&quot; %&gt;
    
    &lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
    pageEncoding=&quot;ISO-8859-1&quot;%&gt;
    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    	&lt;head&gt;
    		&lt;meta charset=&quot;ISO-8859-1&quot;&gt;
    		&lt;title&gt;Insert title here&lt;/title&gt;
    	&lt;/head&gt;
    	&lt;body&gt;
    		&lt;%
    		String user = request.getParameter(&quot;username&quot;);
    		System.out.println(&quot;Try - 05 Trying to establish con in regCheck.jsp&quot;);
    		Connection con = DB.con();
    		System.out.println(&quot;Preparing Statement in regCheck.jsp&quot;);
    		PreparedStatement p = con.prepareStatement(&quot;Insert into register(username)values(?)&quot;);
    
    		p.setString(1,user);
    		System.out.println(&quot;user::&quot; +user);
    
    		int R = p.executeUpdate();
    		if(R!=0)
    			response.sendRedirect(&quot;index.html&quot;);
    		else
    			response.sendRedirect(&quot;register.jsp&quot;);
    
    		%&gt;
    
    	&lt;/body&gt;
    &lt;/html&gt;

</details>


# 答案1
**得分**: 0

JDBC驱动程序库在您的*运行时*CLASSPATH中丢失,即Tomcat在其中查找所需的类以执行已编译代码的CLASSPATH。

<details>
<summary>英文:</summary>

The JDBC driver library is missing from your *runtime* CLASSPATH i.e. the CLASSPATH where Tomcat looks for the required classes in order to execute your compiled code.





</details>



# 答案2
**得分**: 0

我将mysql-connector-java-8.0.19.jar添加到Tomcat 7.0/Lib中,现在它正常工作。
我从库中和所有地方都将它移除,它仍然可用。
我创建了一个新项目,使用相同的代码,而没有将mysql-connector-java-8.0.19.jar添加到任何库或构建路径中,它正常运行。
我在某个地方的YouTube上找到了将其添加到Tomcat的说明。

<details>
<summary>英文:</summary>

I added mysql-connector-java-8.0.19.jar  to Tomcat 7.0/Lib and now it&#39;s working find. 
I removed it from Library and everywhere and it works.
I created new project with the same code and without adding mysql-connector-java-8.0.19.jar to any library or build path, it&#39;s running fine. 
I found the instruction to add to tomcat somewhere on youtube. 

</details>



huangapple
  • 本文由 发表于 2020年3月16日 22:12:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/60707549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定