Olly
Olly

Reputation: 371

java.lang.ClassNotFoundException: "com/mysql/jdbc/Driver" with sql driver in pom

EDIT: Fixed the issue - I wasn't declaring the dependency within the apache tomcat plugin:

<extraDependencies>
                                <extraDependency>
                                    <groupId>org.apache.derby</groupId>
                                    <artifactId>derby</artifactId>
                                    <version>10.1.3.1</version>
                                </extraDependency>
                                <extraDependency>
                                    <groupId>javax.mail</groupId>
                                    <artifactId>mail</artifactId>
                                    <version>1.4</version>
                                </extraDependency>
                                <extraDependency>
                                    <groupId>mysql</groupId>
                                    <artifactId>mysql-connector-java</artifactId>
                                    <version>5.1.38</version>
                                </extraDependency>
                            </extraDependencies>

As well as being declared in the pom as seen below.


Question:

I'm trying to use the mysql jdbc driver in my maven project:

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

But i'm getting:

java.lang.ClassNotFoundException: "com/mysql/jdbc/Driver";
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.ollynural.app.database.retrievedatabase.DatabaseAccessor.returnSummonerDTOFromDatabaseUsingName(DatabaseAccessor.java:53)

EDIT: Tried using the string instead of CLASS, and error is:

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

And DatabaseAccessor returnSummonerDTOFromDatabaseUsingName is:

    String URL = prop.getProperty("URL");
    String USER = prop.getProperty("USER");
    String PASS = prop.getProperty("PASS");
    String CLASS = prop.getProperty("CLASS");
    String TABLE_UNIVERSITY_INFO =     prop.getProperty("TABLE_BASIC_INFO");
    try {
        Class.forName(CLASS);
    } catch (ClassNotFoundException e) {
        System.out.println("Can't find SQL Driver");
        e.printStackTrace();
    }

The properties are:

URL="jdbc:mysql://localhost:3306/league_database_schema";
USER="username";
PASS="password"
CLASS="com.mysql.jdbc.Driver";

The properties are getting to the class fine, but I have no idea why it can't find the class. I'm relatively new to maven, and this was working before i moved over to intellij and eclipse, but any help would be incredibly helpful!

Thank you

Upvotes: 2

Views: 1049

Answers (2)

Joeri Leemans
Joeri Leemans

Reputation: 142

I can't tell by the fragment you posted, but is your dependency declared in the dependencies section of your pom, and not in dependencyManagement?

Upvotes: 0

Alex
Alex

Reputation: 56

I would recommend checking the repository to see if the jar file has actually been installed.

Within the repository, you should find it in: (repository name)/mysql/mysql-connector-java/5.1.38/

There should be a jar file mysql-connector-java-5.1.38.jar and a pom file mysql-connector-java-5.1.38.pom

I believe that should be it for the repository, if anyone has more ideas/corrections please share. Thanks.

Upvotes: 3

Related Questions