java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 在运行时 (eclipse/maven/tomcat)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8106011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:39:47  来源:igfitidea点击:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver during Runtime (eclipse / maven /tomcat)

mysqleclipsetomcatmaven-2jdbc

提问by user977505

I'm getting this error while running tomcat 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver'. I'm using a combination of Eclipse (Indigo, J2EE version) / Maven (m2e-wtp) / Tomcat 7.0. I've included this dependency in my pom file for my web application (build from scratch).

运行 tomcat 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver' 时出现此错误。我正在使用 Eclipse(Indigo,J2EE 版本)/Maven(m2e-wtp)/Tomcat 7.0 的组合。我已经在我的 web 应用程序的 pom 文件中包含了这个依赖项(从头开始构建)。

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>

I do not get any compilation errors in the 'Problems' view but when I run the Tomcat server from the 'Servers' view, I get these errors. It clearly indicates that Tomcat is unable to find the Class and it is classpath configuration error and I was hoping that maven would take care of this.

我在“问题”视图中没有收到任何编译错误,但是当我从“服务器”视图运行 Tomcat 服务器时,我收到了这些错误。它清楚地表明 Tomcat 无法找到该类,这是类路径配置错误,我希望 maven 能解决这个问题。

I looked at other issues related to 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver' but weren't of much help.

我查看了与“java.lang.ClassNotFoundException: com.mysql.jdbc.Driver”相关的其他问题,但没有太大帮助。

I would greatly appreciate any help.

我将不胜感激任何帮助。

/** This is how I load the Driver */

/** 这是我加载驱动程序的方式 */

static {
        DriverAdapterCPDS cpds_Customer = new DriverAdapterCPDS();
        try {                        cpds_Customer.setDriver(productConfig.getProperty("dbcp.connection.customer.driver_class"));

        } catch (ClassNotFoundException e) {
            // log.error("setDriver Exception " + e);
            e.printStackTrace();
        }
               }

回答by duffymo

Tomcat 7 requires that JDBC driver JARs must go in its /lib directory:

Tomcat 7 要求 JDBC 驱动程序 JAR 必须位于其 /lib 目录中:

http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

Search for the word "forget".

搜索“忘记”这个词。

回答by soulcheck

Make sure the driver actually gets copied to your webapp WEB-INF/lib directory and to wtp deploy dir (something like /.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ in your workspace).

确保驱动程序实际上已复制到您的 webapp WEB-INF/lib 目录和 wtp 部署目录(类似于您工作区中的 /.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/)。

I find maven-wtp integration a bit worse than perfect as i stumble upon this problem very often.

我发现 maven-wtp 集成比完美更糟糕,因为我经常偶然发现这个问题。

回答by Ant3G

for this error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

对于此错误:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

you need to "Import com.mysql.jdbc.Driver;" even if its not used till app running.

你需要“导入com.mysql.jdbc.Driver;” 即使它直到应用程序运行才使用。