简体   繁体   中英

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

I'm new to java and I created my project using maven. I have a pom.xml file that looks 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.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>

and my main java program looks roughly/simply like this

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;
        }

    }
}

This compiles correctly with mvn clean package then when I try to run with java -jar target/swingy-1.0-SNAPSHOT.jar I get this error

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

I've looked everywhere for the answer and most articles suggest I "add the .jar file" to my classpath but I don't know what this might mean because everything I've tried like downloading the .jar file to the project path, still nothing. I'm using vscode and my this is my project tree structure

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

you need to add your dependencies in your final jar which can be done by maven using the following plugin:

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

To run your project, you can use the following command:

java -jar target/swingy-1.0-SNAPSHOT-jar-with-dependencies.jar

I hope it works for you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM