Sergii Zhuravskyi
Sergii Zhuravskyi

Reputation: 4358

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver despite the fact it is in dependencies of gradle.build file

I have read top 10-15 questions with answers by the following query https://stackoverflow.com/search?q=com.microsoft.sqlserver.jdbc.SQLServerDriver%22
However, I still don't understand why it doesn't work.
Usual steps to solve this issue:

  1. Make sure that the jar is add to as a dependecy in your build/dependency management tool (Ant/Maven (pom.xml)/Gradle(gralde.build)) Yes, it is in my case:

    dependencies {
        compile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '7.4.0.jre8'
        testCompile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '7.4.0.jre8'
    }
    

    in build.gradle file and gradle build command works without exceptions.

  2. An alternative solution is to download jar file manually from https://learn.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15 and then add it to a classpath. This solution is undesirable i don't want to do something manually that has to be done by a build tool.

So the question is why I am getting the error "java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver" and I cannot see this jar in the dependencies tab enter image description here

despite the fact that the jar is mentioned in my build.gradle file in the dependency section as a compile-time dependency and as a Test time dependency : enter image description here

FYI: That is how it is called in my code:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Upvotes: 0

Views: 1444

Answers (1)

Mark Rotteveel
Mark Rotteveel

Reputation: 109155

It looks like IntelliJ hasn't imported the addition of these dependency. This usually means that the auto-import is disabled.

You can reimport your gradle file by clicking the reimport button in the gradle tab of IDEA. You can enable auto-import by clicking the Gradle Settings button in the gradle tab, and enabling "Automatically import this project on changes in build script files".

On a separate not, you don't need to declare testCompile if you also declare a dependency as compile.

Upvotes: 1

Related Questions