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

Im creating a 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 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 doing it. I even tried downloading mysql-connector and manually adding it to create a project, but it doesn’t change anything.

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

+5
source share
3 answers

You have defined the scope of your dependency as provided . This means that the jar is used when compiling the project (although you do not need to compile it, since you should use only standard JDBC classes), but that it is not included in the jar or war created by the assembly, because it is assumed that a dependency should be provided The application server where you deploy the application.

So, either you intend to provide this jar, and it should be in the path to the application server class, or you want to associate this jar with the application, and it should have a runtime scope (because you need it to run the application, but not to compile it ) instead of provided .

+19
source

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

Deploy 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 Heroku Toolbelt installed, run:

 $ mvn heroku:deploy 

Deploy 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 Heroku Toolbelt installed, run:

 $ mvn heroku:deploy-war 
+2
source

You can very easily fix your problem. This is not a compilation problem, it is clearly a runtime problem, you must add the jar file to your path to the system class. If your package is in .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 / directory.

This will determine your problem.

+1
source

Source: https://habr.com/ru/post/1201018/


All Articles