4

Im building Maven Java Web application and when I do

Class.forName("com.mysql.jdbc.Driver");

I get

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

mysql-connector is added to my pom.xml file like this

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.32</version>
    <scope>provided</scope>
</dependency>

But I keep getting this. I even tried downloading mysql-connector and adding it manually to project, but it doesn't change anything.

I also have the same app without Maven, and same code works fine

ToYonos
  • 16,469
  • 2
  • 54
  • 70
Vuk Stanković
  • 7,864
  • 10
  • 41
  • 65

4 Answers4

18

You have set the scope of your dependency as provided. This means that the jar is used when compiling the project (although you shouldn't need it to compile it, since you should only use standard JDBC classes), but that it's not included in the jar or war created by the build, because this dependency is supposed to be "provided" by the application server where you deploy the application.

So, either you actually intend to have this jar provided, and it should be in the application server's classpath, or you want to bundle this jar with the application, and it should have the scope runtime (because you need it to run the app, but not to compile it), instead of provided.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    Here is the thing. When I set it to `runtime` it works on my local machine, but when I want to deploy it to Heroku, I get an error like `package com.mysql.jdbc does not exist` – Vuk Stanković Aug 24 '14 at 13:43
  • 2
    @VukStanković you should include that deployment information in your question. – Steffen Harbich Mar 29 '18 at 11:09
  • @VukStanković I agree, JB Nizet answer should provide proper guidance. Your second issue (package does not exists) is different than the first one, it's probably a different problem. If you include your build data (your pom.xml and how you build and deploy your app) we should be able to help – Pierre B. Mar 30 '18 at 09:21
  • @VukStanković maybe you want to see, how to use MySQL with Heroku: https://stackoverflow.com/questions/15191259/how-to-deploy-local-mysql-database-to-heroku#15191367 or https://devcenter.heroku.com/articles/heroku-mysql (Heroku does not have out-of-the-box support for MySQL.) – cyberbrain Apr 01 '18 at 12:32
1

You need to add Heroku plugin as maven dependencies to get maven dependencies added at heroku.

Deploying Standalone Applications

<build>
  <plugins>
    <plugin>
      <groupId>com.heroku.sdk</groupId>
      <artifactId>heroku-maven-plugin</artifactId>
      <version>2.0.1</version>
      <configuration>
        <appName>${heroku.appName}</appName>
        <processTypes>
          <web>java $JAVA_OPTS -cp target/classes:target/dependency/* Main</web>
        </processTypes>
      </configuration>
    </plugin>
  </plugins>
</build>

Now, if you have the Heroku Toolbelt installed, run:

$ mvn heroku:deploy

Deploying WAR Files

<build>
  <plugins>
    <plugin>
      <groupId>com.heroku.sdk</groupId>
      <artifactId>heroku-maven-plugin</artifactId>
      <configuration>
        <appName>${heroku.appName}</appName>
      </configuration>
    </plugin>
  </plugins>
</build>

Now, if you have the Heroku Toolbelt installed, run:

$ mvn heroku:deploy-war
positivecrux
  • 1,307
  • 2
  • 16
  • 35
1

I like the answers above. I wrestled with the same problem on my Windows 10 machine using the -cp or -classpath switch with java. But in the end, since I wanted the com.mysql.jdbc.Driver available for all my runnable classes, I just created the CLASSPATH environmental variable in the machine's environmental variable list. You have to be sure and add the current directory path . to the list otherwise the JVM can't find the class [for the main function] when you issue the command "java MainClassName" at the command prompt.

I am using eclipse and adding the external jar files to a project is easy and the projects run fine in the IDE without the CLASSPATH variable defined, but they still don't run in a command prompt window without the CLASSPATH variable defined. I also found that with the UcanAccess [a solution for ODBC in JE 8 and greater] driver I had to include all 5 jar files in the CLASSPATH variable.

I haven't tried building an executable jar file yet to see if I need the CLASSPATH variable. That is my next item to check.

0

Very simply you can fix your problem. This is not a compilation issue, it is clearly a run time issue, you must add the jar file in your system class path. If your package is in a .war or .ear format you can fix this by changing the maven configuration from <scope>provided</scope> to <scope>local</scope>. This correction will add the jar to your local lib folder/directory.

This will definitely fix your issue.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17