Maven - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29245102/
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-11 07:39:02  来源:igfitidea点击:

Maven - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

javamysqleclipsemaven

提问by pokero

I have a Java app based on Maven, and want to connect to MySQL server.

我有一个基于 Maven 的 Java 应用程序,想连接到 MySQL 服务器。

My pom has:

我的 pom 有:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.17</version>
    <type>jar</type>
    <scope>runtime</scope>
</dependency>

With runtime, as I want to connect to MySQL server at runtime - have also tried compile and provided, but does not work.

使用runtime,因为我想在运行时连接到 MySQL 服务器 - 也尝试过编译和提供,但不起作用。

The SQL code is standard:

SQL 代码是标准的:

String dbClass = "com.mysql.jdbc.Driver";

Class.forName(dbClass);
Connection connection = DriverManager.getConnection(dbUrl,
    username, password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
    String tableName = resultSet.getString(1);
    System.out.println("Table name : " + tableName);
}

When I run this from Eclipse, it works fine and prints table names.

当我从 Eclipse运行它,它工作正常并打印表名。

However, from maven, the generated SNAPSHOT always gives an error when executed via >java -jar target\File.jarafter running mvn clean install.

但是,从 maven 中,生成的 SNAPSHOTjava -jar target\File.jar在运行后通过 > 执行时总是会出错mvn clean install

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

What am I missing here to get the maven build to work? Running mvn clean installgives no error and builds fine. It is only when executing the SNAPSHOT exe the error happens.

我在这里缺少什么才能使 Maven 构建正常工作?运行mvn clean install没有错误并且构建良好。只有在执行 SNAPSHOT exe 时才会发生错误。

The MySQL jar is in my .m2 repo, and I tried adding it explicitly via mvn command line, but says it already exists.

MySQL jar 在我的 .m2 存储库中,我尝试通过 mvn 命令行显式添加它,但说它已经存在。

回答by JBA

Change the scope to compile:

将范围更改为compile

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.17</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

Which - since it is the default scope corresponds to leaving away scope definition at all - same counts for the type:

其中 - 因为它是默认范围对应于完全不考虑范围定义 - 类型的相同计数:

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

Have a look at this: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.htmlfor detailed information on scoping.

看看这个:https: //maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html有关范围界定的详细信息。

Here is a quick info for your background:

以下是您背景的快速信息:

You specified the JDBC driver to have a scope runtime. Most IDEs will anyways ignore the scopes and add all of your dependencies to theirclasspath (e.g. the classpath used when you run something outside of eclipse. By the scope runtimeyou are telling maven that it must not pack that dependeny into your final jar since the execution environment will "provide that dependency at runtime. E.g. you would either have to manually add it to the classpath when calling your jar or change the scope to compilewhich will lead to the driver-jar beeing packed inside your jar and available at runtime.

您指定 JDBC 驱动程序具有范围runtime。大多数 IDE 无论如何都会忽略作用域并将所有依赖项添加到它们的类路径中(例如,当您在 eclipse 之外运行某些东西时使用的类路径。通过作用域,runtime您告诉 Maven 自执行以来它不能将该依赖项打包到您的最终 jar 中环境会“提供的这种依赖性runtime。例如,你要么有呼唤你的罐子时,手动将其添加到类路径或更改范围,compile这将导致驾驶员-JAR beeing你的包装罐内,并在运行时可用。

回答by pokero

The answer is here - How can I create an executable JAR with dependencies using Maven?

答案就在这里 -如何使用 Maven 创建具有依赖项的可执行 JAR?

I needed to build an uber pom, using the answer in the link above - this builds in the dependencies (in this case the mysql jar file) into a single SNAPSHOT jar file.

我需要使用上面链接中的答案构建一个 uber pom - 这将依赖项(在本例中为 mysql jar 文件)构建为单个 SNAPSHOT jar 文件。

Just make sure you run it with mvn clean compile assembly:single(not the usual mvn clean packageor whatever.

只要确保你运行它mvn clean compile assembly:single(不是通常的mvn clean package或其他什么。

回答by Faxriddin Abdullayev

Add this code

添加此代码

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

to porm.xml file in your maven project

到 maven 项目中的 porm.xml 文件

回答by Khosro Makari

Since you are running the project by "java -jar" and also you have dependencies, so, you have to use two maven plugins. The first one to copy dependencies into a folder inside target folder (e.g. lib/) while packaging and the second one for specifying the classpath which should be same as the first one(lib/). I had the same problem and here is what I did:

由于您通过“java -jar”运行项目并且您有依赖项,因此,您必须使用两个 maven 插件。第一个在打包时将依赖项复制到目标文件夹(例如 lib/)内的文件夹中,第二个用于指定应与第一个(lib/)相同的类路径。我遇到了同样的问题,这就是我所做的:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>

                  <mainClass>com.tihoo.crawler.Application</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

回答by Chris

I had this problem as well. I tried using plugins to set the connector to classpath but it didn't work. I tried to change versions, it didn't work. I've spent two nights to find out that i needed to specify in pom.xml that i need my package to be a "war".

我也有这个问题。我尝试使用插件将连接器设置为类路径,但没有用。我试图改变版本,它没有用。我花了两个晚上才发现我需要在 pom.xml 中指定我需要我的包是“战争”。

Maybe it's because your project doesn't know what type it is, so try to put this:

也许是因为你的项目不知道它是什么类型,所以试着把这个:

<packaging>war</packaging>

right next to your project version. Something like this:

就在您的项目版本旁边。像这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>example-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <!-- Here -->

<properties>
    <!-- Some properties here -->
</properties>

<dependencies>
    <!-- Some dependencies here -->
</dependencies>

Maybe it doesn't suit your problem, but maybe somebody else will need it.

也许它不适合您的问题,但也许其他人会需要它。