java - Maven java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
问题描述
我是 java 新手,我使用 maven 创建了我的项目。我有一个pom.xml
看起来像这样的文件
<?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.swingy.app</groupId>
<artifactId>swingy</artifactId>
<version>1.0-SNAPSHOT</version>
<name>swingy</name>
<!-- FIXME change it to the project's website -->
<url>http://maven.apache.org</url>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
</dependencies>
</project>
我的主要java程序看起来大致/简单地像这样
package com.swingy.app;
import java.sql.*;
public class App {
static Connection conn = null;
static PreparedStatement pStatement = null;
public static void main(String[] args) {
makeJDBCConnection();
}
private static void makeJDBCConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "Pass");
if (conn != null) {
System.out.println("Connection Successful! Enjoy. Now it's time to push data");
} else {
System.out.println("Failed to make connection!");
}
} catch (SQLException e) {
System.out.println("MySQL Connection Failed!");
e.printStackTrace();
return;
}
}
}
这可以正确编译,mvn clean package
然后当我尝试运行时出现java -jar target/swingy-1.0-SNAPSHOT.jar
此错误
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
我到处寻找答案,大多数文章都建议我"add the .jar file"
使用我的类路径,但我不知道这可能意味着什么,因为我尝试的所有操作都像将.jar
文件下载到项目路径一样,仍然没有。我正在使用 vscode,这是我的项目树结构
.
├── pom.xml
├── src
│ ├── main
│ │ └── java
│ │ └── com
│ │ └── swingy
│ │ ├── app
│ │ │ ├── App.java
│ │ │ └── Main.java
│ │ ├── controls
│ │ ├── models
│ │ │ └── Player.java
│ │ └── views
│ │ └── Colors.java
│ └── test
│ └── java
│ └── com
│ └── swingy
│ └── app
│ └── AppTest.java
└── target
├── classes
...
解决方案
您需要在最终的 jar 中添加依赖项,这可以通过 maven 使用以下插件来完成:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.swingy.app.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
要运行您的项目,您可以使用以下命令:
java -jar target/swingy-1.0-SNAPSHOT-jar-with-dependencies.jar
我希望这个对你有用 :)
推荐阅读
- android - 有没有办法强制 Glide 只从缓存加载图像,而不是从网络加载图像?
- typescript - 打字稿中是否有像 python 中的 __setattr__ 这样的函数?
- string - 使用Matlab查找字符串中包含的子字符串
- c# - 使用未分配的变量“格式”、“attr1”、“attr2”、“attr3”
- javascript - 通过 Firestore 文档的对象数组“映射”时出错。(React-Redux)
- python - model.predict() - 精度接近 1 的模型预测错误的类别
- amazon-web-services - 如何控制 Hive-On-Tez 中的容器数量
- r - 重复循环中 data.table v1.13.0 的性能问题
- python - 使用 regex_extract 循环遍历字符串列表 Pyspark
- c++ - 使用 libvpx 的实时 VP9 编码