I am trying to create a maven version of the gradle-based treehouse 'unit-testing-a-spring-application' project used in this course. See course code here.
After not being able to build initially, I ended up adding dependency versions specified in the original gradle build file. Maven will build fine, but running the app gives this:
The following method did not exist:
org.springframework.data.repository.config.RepositoryConfigurationSource.getAttribute(Ljava/lang/String;)
Ljava/lang/String;
The method's class, org.springframework.data.repository.config.RepositoryConfigurationSource, is
available from the following locations:
jar:file:/C:/Users/home/.m2/repository/org/springframework/data/spring-data-
commons/2.1.6.RELEASE/spring-data-commons-2.1.6.RELEASE.jar!/org/springframework/data/repository/config/RepositoryConfigurationSource.class
It was loaded from the following location:
file:/C:/Users/home/.m2/repository/org/springframework/data/spring-data-commons/2.1.6.RELEASE/spring-
data-commons-2.1.6.RELEASE.jar
Action
Correct the classpath of your application so that it contains a single, compatible version of
org.springframework.data.repository.config.RepositoryConfigurationSource.
Several similar problems are solved here on S.O. by changing the version of some related dependency. I have tried various versions of the following to no avail (I stick normally with spring-boot-starter-parent v2.1.4 cause it works all the time with my projects):
spring-data-commons
spring-boot-starter-parent
I see the needed class below listed in my project Dependencies directory, but what dependency versions will work in this case? How can I solve this otherwise?
org/springframework/data/repository/config/RepositoryConfigurationSource.class
I looked at your code and found some mistakes. Since your project is a spring-boot one, you mustn't add direct spring dependencies in your pom file. Instead try to use spring-boot based dependencies. For instance, instead of having such this dependency:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.2.RELEASE</version>
</dependency>
You must have the following one in your pom file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
OK I am trying to use http://jongo.org, in a project I have run mvn install and dropped the jar it returns into my project but when I try and use Jongo I get Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/Module Does Jongo not package these directly?
My question is do I need to then manually include the other jars it depends on aswell into my project root?
Yes, you need add this dependency manually in maven:
Jongo relies upon Jackson 2.4.1, Bson4Jackson 2.4.0 and Mongo Java Driver 2.11+. Its Maven dependency, an OSGI compliant jar, comes with
the first two, you have to provide a dependency to the driver.
Jongo is deployed into OSS Sonatype (Maven repository hosting service
for open source projects). Add the following dependency to your
pom.xml
<dependency>
<groupId>org.jongo</groupId>
<artifactId>jongo</artifactId>
<version>1.2</version> </dependency>
</dependency>
spring.freemarker.cache:false
spring.datasource.url=jdbc:mysql://localhost/mydb
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
im getting this error even with the driver-class-name difined
java.lang.IllegalStateException: Cannot load driver class:
com.mysql.jdbc.Driver at
org.springframework.util.Assert.state(Assert.java:392)
~[spring-core-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:153)
~[spring-boot-autoconfigure-1.3.0.M5.jar:1.3.0.M5] at
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:119)
~[spring-boot-autoconfigure-1.3.0.M5.jar:1.3.0.M5] at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
~[na:1.8.0_60] at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
~[na:1.8.0_60] at ......
and it goes on
If you're using Maven, add this to your pom.xml:
(Recommended) For MySQL 5.6, 5.7, 8.x and Java >= 8 use;
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
(Legacy) For MySQL <= 5.5 or Java <= 7 or JDBC < 4.2 use;
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
More details on Connector/J versions
It might be that you are using the deprecated driver class name. I solved it by updating the property in application.properties file.
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Just confirm the following things.
If you are under proxy make sure use VPN to connect to internal servers, iif you are accessing DEV/STG servers
Make sure you add the following dependency inside correct location, like this
<dependencies> <--- inside this section
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
and Not in
`<build> <--- Not this section
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
`
3. And you don't need to use the following line
`spring.datasource.driver-class-name=com.mysql.jdbc.Driver.
The spring.datasource.url=jdbc:mysql://xxxxx` automatically know which driver to fetch.
If you are using IDEA and maven, you might forget reloading all maven projects, which causes the dependencies don't have the MySQL connector jar. And that's why when u run the application, there will be the error remind "com.mysql.cj.jdbc.Driver".
The crux of the problem is that you're missing MySQL driver dependency.
One of the ways, as outlined by other answers, is to specify it in your build tool's configuration. However, if you do not want to do it and are using IntelliJ IDEA (though I'm pretty sure Eclipse has something similar), you can also add the dependency via it. Steps:
Download desired jar from maven repository
Open your project in Intellij IDEA
File -> Project Structure -> Libraries
Click New Project Library (green plus sign on the left), or press Alt + Insert keys
Select Java
Select your jar
Press OK
Should look something like this:
Now you should be able to use the dependency you have just added.
Tested with IntelliJ IDEA 2017.3.4.
I will share more causes of this error:
I already seen this error many times using Intellij IDEA and running some integration test. The test fail with the message: Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver. After rebuild the project (Build > Rebuild Project) the error disappears.
Another cause of this error is a corrupted JAR. A college was receiving this error (also trying to run integration tests) and, after delete some related jars on .m2 directory, the error was gone.
build.gradle
runtimeOnly 'mysql:mysql-connector-java'
application.yml
spring:
application:
name: apevent
version: 1.0
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/applicaiton
Probably someone still needs an answer (as me before) I solved it by adding the following dependency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
I encountered the same problem now and solved it after providing the required dependency,
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
adding the following dependency solved my issue
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
I know this might be a little late but maybe anyone from the future might find this answer helpful.
This problem arises from potentially three reasons
Incorrect maven or Gradle configuration
Incorrect spring configuration in the application.properties file
Wrong class name on the application.properties file.
To troubleshoot if you are using maven. do not include the version name. This mostly is a recipe for problems, especially in huge applications. use maven autocompletes features to save you a little time.
configure the data source correctly. There are a lot of answers on it to this question. make sure it's correct. sometimes there is a cj that is missing or included where it's not supposed to be. Trial and error might help you discover the problem
if neither of the above options works. Remove spring.datasource.driver-class-name=com.mysql.jdbc.driver completely. I don't know if it magic but this last one worked for me.
GO NERDS!!!
My problem is solved just by refresh the project, perhap the dependency did not load by the time you run the application
I had the same issue. It happened because I forgot to declare the MySQL connector dependency in my pom file. I added the dependency, then boom! The error went away.
For People who uses Gradle 8.0 and beyond. I had to use
implementation 'mysql:mysql-connector-java:8.0.26'
instead of
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.26' ,
my application.yml looks like:
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/<Your DB name>?useSSL=false
remember to reload your gradle file
I need to add cxf-core, cxf-frontend ext. file in the project. I am trying to add these using maven dependency in pom.xml but it didn't work and version number is getting be red when it is added.
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>2.6.2</version>
</dependency>
org.apache.cxf:cxf-rt-core:2.6.2 not found.
I also tried to add using external .jar libraries. The IDE is Intellij IDEA.
2.6.2 worked for me, I used Eclipse to create a new maven project and pasted in your dependency. It worked, fetched the required jars and give me this dependency hierarchy.
I am very new to eclipse and maven repository. I got a project to config the errors of POM.xml. I fixed many of it, but I am unable to fix some. Following are that errors.
<dependency>
<groupId>jlibs</groupId>
<artifactId>jlibs-jdbc</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.jgodies.form</groupId>
<artifactId>forms</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
<scope>runtime</scope>
In Maven repository folder there are not all the files which are in the other folders, so I put them manually, but still no results.
All the Oracle realted jars are not the part of the Central Maven Repository.
May be the case with the other oracle related jars. That is the reason why its not able to resolve the dependencies.
You should create a separate custom repository on your company server where you can put all these jar and configure that in POM.xml. Maven will first check the Central reposiroty and if it does not find the files will later check the custom repositry.
For locally running Maven you will have to do a MVN Install manually. you should never manuallly copy any jars to the m2 repositiry
Getting the JDBC drivers is a bit tricky. Please check this article for directions.
Due to Oracle license restriction, there is NO public Maven repository provides Oracle JDBC driver. To use Oracle jdbc drive with Maven, you have to install it manually into your Maven local repository.