com.microsoft.sqlserver.jdbc.SQLServerDriver class not found exception (NEW) - java

Now I am working with Microsoft SQLserver jdbc connectivity when I try to define class.forname
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
error occurs,
everything else is fine, but I dint have any idea about this error.
pom.xml:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4-2.0</version>
<scope>system</scope>
<systemPath>D:\my current work(please backup this folder)\backup\12-15-2016\milma_jishnu/src/main/lib/sqljdbc4-2.0.jar</systemPath>
</dependency>

Quoting Maven documentation:
System Dependencies
Important note: This is marked deprecated.
Dependencies with the scope system are always available and are not looked up in repository. They are usually used to tell Maven about dependencies which are provided by the JDK or the VM. Thus, system dependencies are especially useful for resolving dependencies on artifacts which are now provided by the JDK, but where available as separate downloads earlier. Typical example are the JDBC standard extensions or the Java Authentication and Authorization Service (JAAS).
So, <scope>system</scope> tells Maven: This is already present, you don't need to do anything.
Ergo, it is not added to the classpath, and you wonder why it's not there?
Seems you misunderstood the purpose of <scope>system</scope>. Don't use it, because the MS SQL JDBC driver is not part of the JDK.

Related

Spring, Maven: Correct the classpath of your application

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>

Cannot load driver class: com.mysql.jdbc.Driver Spring

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

Neo4j No query engine installed

I tried running the Neo4j sample program JavaQuery.java. I used the neo4j maven script provided to get all the dependencies. However when I run it I get the following error:
Exception in thread "main" java.lang.UnsupportedOperationException: No query engine installed.
at org.neo4j.kernel.impl.query.NoQueryEngine.noQueryEngine(NoQueryEngine.java:56)
at org.neo4j.kernel.impl.query.NoQueryEngine.executeQuery(NoQueryEngine.java:33)
at org.neo4j.kernel.InternalAbstractGraphDatabase.execute(InternalAbstractGraphDatabase.java:953)
at org.neo4j.kernel.InternalAbstractGraphDatabase.execute(InternalAbstractGraphDatabase.java:944)
at JavaQuery.JavaQuery.run(JavaQuery.java:60)
at JavaQuery.JavaQuery.main(JavaQuery.java:44)
I haven't found any documentation about how to get the query engine installed.
the simple low level java interface seems to work, but I haven't been able to get Cypher queries to work.
How do I get past this issue?
You need neo4j cypher dependency.
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-cypher</artifactId>
<version>2.2.0</version>
</dependency>
The neo4j-cypher dependency that #shazin mentioned, should come along when you depend on:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>2.2.0</version>
</dependency>
I had a similar issue and I had the dependency org.neo4j:neo4j:* configured. Here are some ideas.
Cause #1 : Dependency shadowing
It could be one of your dependencies conflicting with transitive dependency from neo4j. Putting neo4j higher in your SBT could solve this issue.
(This was what worked for me)
Cause #2 : Maximum command line reach (Windows only)
Neo4j has tons of jars. It could be the java command that is too long for windows' maximum number of characters. See: How to set a long Java classpath in Windows?

javax.validation.ValidationException: Unable to find default provider

I am currently working on Spring MVC web app and trying to hook up validation using the #Valid annotation. When I fire up the application I'm getting the following exception:
javax.validation.ValidationException: Unable to find a default provider
I have Hibernate Validator 3.1.0.GA on the classpath as well as javax validation 1.0.0.GA, Hibernate Core 3.3.1.GA and Hibernate Annotations 3.4.0.GA.
Is there an incompatiblity in those versions that I'm not seeing, or can anyone think of any reason why I'm still getting this exception with Hibernate Validator on the class path?
Cheers,
Caps
See this answer : https://stackoverflow.com/a/3989936/325742
To fix, Add this maven dependency Hibernate Validator Annotation Processor.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>4.1.0.Final</version>
</dependency>
That's the latest stable version of that artifact, as seen from here
Generic way of finding a dependency
Let's say that you got a a NoClassDefFoundError stating that the class org.postgresql.Driver was not found.
Use Jarvana to search for a dependency that can provide org.postgresql.Driver like so : http://www.jarvana.com/jarvana/search?search_type=class&java_class=org.postgresql.Driver which gives
Translate the above dependency into maven dependency format :
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
Confirm that the above is available at Maven Central by searching like this : g:"postgresql" AND a:"postgresql"
(where g stands for GroupID and a stands for artifactID)
Finally, add it to your pom.xml
Finding Dependencies using m2e
If you have an approximate idea of the dependency needed or can figure out the one you need given a list, then m2e's dependency search can be the quickest way of adding a dependency to your pom.xml
Steps :
Click on the Dependencies tab (A) in your pom.xml
Click on Add (B)
Search for the dependency by groupId/artifactId (C)
Double click the required one from the search results to have it added directly to your pom.xml (D)
A-D marked in the following snapshot :
Finding dependencies in IntelliJ Idea
In IntelliJ, looking up a dependency is much easier. All you need to do, is to make sure that the maven central repo has been indexed by IntelliJ like so:
And then, go into the pom, do a dep+Tab (or an Alt+Insert as shown here), and this is what you get:
If you are in a class which has an unresolved import, then the quick fix gives you an option of searching and adding the corresponding maven repo by doing an Alt+Enter on the missing Class/Package:
Awesome I say !
Hibernate Validator 3.1 is not a JSR303 provider. You need to upgrade to Hibernate Validator 4 or later.
This happened to me without Hibernate.
Running great on my PC, it didn't work on my EC2 Linux server.
The reason was an existence of validation-api-1.0.0.GA.jar file under /usr/share/tomcat/lib.
Once validation-api-1.0.0.GA.jar deleted, it worked great.
In my case, I had the same problem, but it was happening because the jar of hibernate-core version 4.1.8.Final downloaded by maven was corrupted. I swithed to version 4.1.6.Final and it started working. I was using STS and spring repositories.
Hope this helps someone.
In same Situation i update my Jar version only for anotations from hibernate-anotation and hibernate-common-annotation to Hibernate4 anotations which are listed below.
For Hibernate 4 you can use this jars-
1-hibernate-commons-annotations-4.0.5.Final
2-hibernate-validator-4.2.0.Final
Hope this will work for you also.
I received the following error:
Invocation of init method failed; nested exception is javax.validation.ValidationException: Unable to get available provider resolvers
I discovered that in my WEB-INF/lib directory I had two versions of Validator.class present in both of the following jar files:
org.springframework.context-3.1.1.RELEASE.jar
com.springsource.javax.validation-1.0.0.GA.jar
I removed the com.springsource.javax.validation-1.0.0.GA.jar from the WEB-INF/lib directory because it is older and no longer supported. After doing so, my application worked perfectly. I learned from other posts that my problem had something to do with duplicate versions of the same file on the classpath. I figured out which file was causing the problem after reading Spring 3 Validation

Maven dependency exclusion for War file, but inclusion for tests

I have a maven POM file for a web service. For one of the dependencies I have to specify several exclusions for jar files that are already kept at a higher-level in the web-application server (accessible to all web-applications, not just this particular one). One example of such exclusion is the JAR containing my JDBC driver.
Example (with fictional details):
<dependency>
<groupId>mygroup</groupId>
<artifactId>myartifact</artifactId>
<version>1.0.0</version>
<exclusions>
<!--The jdbc driver causes hot-deployment issues-->
<exclusion>
<groupId>db.drivers</groupId>
<artifactId>jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
The problem I am encountering is that I need the JDBC driver for my tests. My tests currently fail since they cannot load the JDBC driver.
How can I configure the POM so that the excluded parts are accessible to my tests, but do not get included into my WAR file?
Update: I cannot make changes to the POM for mygroup.myartifact since this it is being depended on by many other projects, and this exclusion requirement is unique for my project.
Update 2: It seems I did a poor job of phrasing this question. Lars's solution below is perfect for one exclusion (as the example shows), however in my real scenario I have multiple exclusions, and adding additional dependencies for each seems smelly. The solution that seems to work is to set the scope of the shown dependency to compile and then create a second dependency the same artifact (mygroup.myartifact) with no exclusions and the scope set to test. Since Lars both answered my poorly phrased question correctly, as well as led me in the direction of the actual solution, I will mark his reply as the answer.
Use the "scope" tag inside your dependency.
<scope>test</scope>
http://maven.apache.org/pom.html#Dependencies
edit: if I understand your configuration correctly, the scope=test that you need to add should be added in the mygroup.myartifact POM. That way you can test that artifact with jdbc jar included, but always when other POMS want to include mygroup.myartifact, they don't get jdbc included as a transitive dependency.
second edit: Ok, if you don't control the POM you want to include - do an exclusion like you have already done, and then add jdbc as a new dependency, with scope=test.

Categories