当前位置: 首页 > 知识库问答 >
问题:

Tomcat:java.sql。SQLException:无法加载类:com.mysql.jdbc。来自类加载器的驱动程序

阚英睿
2023-03-14

在服务器上运行Web应用程序时,我遇到以下异常:

使用的服务器:ApacheTomcat 8.5

异常:java.sql.SQLException:无法从类加载器加载class: com.mysql.jdbc.Driver

我尝试了很多事情,包括将ojdbc6.jar放在WEB-INF / lib文件夹中,将jar放入apache tomcat lib文件夹中,在项目的构建路径中,我已经手动添加了jar,也基于Google的发现尝试了几个周转,这在我的情况下没有成功。

从web应用程序的角度来看,类或资源加载按以下顺序查看存储库:

  1. JVM 的引导类
  2. /WEB-INF/Web 应用程序的类
  3. /WEB-INF/lib/*web 应用程序的.jar
  4. 系统类装入器类(如上所述)
  5. 常见类装入器类(如上所述)

我将jar放在/WEB-INF/lib/文件夹中。

完整的堆栈跟踪。

java.sql.SQLException: Unable to load class: com.mysql.jdbc.Driver from ClassLoader:java.net.URLClassLoader@76ed5528;ClassLoader:ParallelWebappClassLoader
  context: ROOT
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@76ed5528

    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:283)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:718)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:650)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:468)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:143)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.createDataSource(DataSourceFactory.java:554)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:241)
    at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:94)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:839)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1091)
    at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:661)
    at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:249)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5087)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1404)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1394)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: Unable to load class: com.mysql.jdbc.Driver from ClassLoader:java.net.URLClassLoader@76ed5528;ClassLoader:ParallelWebappClassLoader
  context: ROOT
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@76ed5528

    at org.apache.tomcat.jdbc.pool.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:56)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:271)
    ... 25 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.apache.tomcat.jdbc.pool.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:38)
    ... 26 more

May 07, 2019 4:04:12 PM org.apache.naming.NamingContext lookup
WARNING: Unexpected exception resolving reference
java.sql.SQLException: Unable to load class: com.mysql.jdbc.Driver from ClassLoader:java.net.URLClassLoader@76ed5528;ClassLoader:ParallelWebappClassLoader
  context: ROOT
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@76ed5528

    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:283)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:718)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:650)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:468)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:143)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.createDataSource(DataSourceFactory.java:554)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:241)
    at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:94)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:839)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1091)
    at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:661)
    at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:249)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5087)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1404)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1394)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: Unable to load class: com.mysql.jdbc.Driver from ClassLoader:java.net.URLClassLoader@76ed5528;ClassLoader:ParallelWebappClassLoader
  context: ROOT
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@76ed5528

    at org.apache.tomcat.jdbc.pool.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:56)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:271)
    ... 25 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.apache.tomcat.jdbc.pool.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:38)
    ... 26 more

May 07, 2019 4:04:12 PM org.apache.catalina.core.NamingContextListener addResource
WARNING: Failed to register in JMX: javax.naming.NamingException: Unable to load class: com.mysql.jdbc.Driver from ClassLoader:java.net.URLClassLoader@76ed5528;ClassLoader:ParallelWebappClassLoader
  cont8ext: ROOT
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@76ed552

预期结果:它不能抛出jar的任何异常,因为jar已经存在于 /WEB-INF/lib/以及tomcat/lib文件夹中。

实际结果:如上所述,它正在抛出异常。

共有3个答案

冯风史
2023-03-14

我在 Debian 10 中处理 Java 项目时遇到了此错误。

问题是我有多个mySQL JDBC连接器.jar文件,即:

mysql-connector-java-5.1.27.jar
mysql-connector-java-5.1.27.jar-1

以下是我修复它的方法:

我只是从主文件开始删除了两个文件:

sudo rm -rf mysql-connector-java-5.1.27.jar
sudo rm -rf mysql-connector-java-5.1.27.jar-1

然后我再次将文件的副本下载到tomcat(/opt/tomcat/lib)的lib目录中:

sudo wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.27/mysql-connector-java-5.1.27.jar

一切又恢复正常了。

仅此而已。

我希望这有所帮助

李跃
2023-03-14

我想在更多的头脑Storm后发布一个答案(至少对我来说)。

WEB-INF/lib文件夹应该包含这两个jar,如果在某些奇怪的情况下(我的情况),类加载器没有从那里选择它,请将它复制到tomcat/lib文件夹中。

它为我工作,所以发布一个答案,我对任何其他观点持开放态度。

温嘉玉
2023-03-14

您显然正在尝试从Oracle JDBC JAR文件(ojdbc6.jar)加载MySQL JDBC驱动程序类(com.mysql.jdbc.Driver

这是行不通的。

如果您尝试与 Oracle 数据库通信,则需要使用 Oracle JDBC 的驱动程序类名。

如果您尝试与MySQL数据库通信,则需要使用适当的JConnector驱动程序。

根据您的评论,问题是您将JDBC驱动程序JAR文件放在了错误的位置。如果您使用Tomcat的JNDI数据源配置机制,驱动程序JAR需要位于$CATALINA_HOME/lib...根据说明。

这是因为驱动程序是由核心Tomcat代码加载的,而不是由webapp加载的。您的webapp的< code>WEB-INF/lib目录不会在核心的类加载器的类路径上。

 类似资料: