Maven - java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [英] Maven - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

查看:114
本文介绍了Maven - java.lang.ClassNotFoundException:com.mysql.jdbc.Driver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的pom有:

 <依赖关系> 
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 5.1.17< / version>
< type> jar< / type>
< scope>运行时< / scope>
< / dependency>

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



SQL代码是标准的:

  String dbClass =com.mysql.jdbc.Driver; 

Class.forName(dbClass);
连接连接= DriverManager.getConnection(dbUrl,
username,password);
语句语句= connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
while(resultSet.next()){
String tableName = resultSet.getString(1);
System.out.println(表名:+ tableName);
}

当我从Eclipse 运行这个但是,从maven,生成的SNAPSHOT通过> java -jar target\File执行时总是发生错误。 .jar 运行 mvn clean install


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


我在这里缺少什么来让maven构建工作?运行 mvn clean install 不会发生错误并且建立好的。只有在执行SNAPSHOT EXE时才会发生错误。



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

解决方案

将范围更改为 compile p>

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

其中 - 由于默认范围对应于删除范围定义完全相同 - 键入:

 <依赖关系> 
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 5.1.17< / version>
< / dependency>

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



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



您指定了JDBC驱动程序的范围运行时。大多数IDE将无视忽略范围,并将所有依赖项添加到他们的类路径中(例如,当您运行eclipse之外的东西时使用的类路径)。范围 runtime 你告诉maven,它不能将它包装到你的最终的jar中,因为执行环境将在运行时提供依赖关系,例如你必须手动将其添加到类路径中,调用您的jar或将范围更改为 compile ,这将导致驱动程序jar包装在您的jar中,并在运行时可用。


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

My pom has:

<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.

The SQL code is standard:

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.

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

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

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

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

解决方案

Change the scope to 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.html for detailed information on scoping.

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 their classpath (e.g. the classpath used when you run something outside of eclipse. By the scope runtime you 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 compile which will lead to the driver-jar beeing packed inside your jar and available at runtime.

这篇关于Maven - java.lang.ClassNotFoundException:com.mysql.jdbc.Driver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆