简体   繁体   中英

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://docs.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 在此处输入图像描述

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: 在此处输入图像描述

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM