[英]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我已通过以下查询https://stackoverflow.com/search?q=com.microsoft.sqlserver.jdbc.SQLServerDriver%22阅读了前 10-15 个问题的答案
However, I still don't understand why it doesn't work.但是,我仍然不明白为什么它不起作用。
Usual steps to solve this issue:解决此问题的常用步骤:
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:确保将 jar 作为依赖项添加到您的构建/依赖项管理工具(Ant/Maven (pom.xml)/Gradle(gralde.build))中是的,就我而言:
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.在build.gradle文件和gradle 构建命令中没有例外。
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.另一种解决方案是从https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-手动下载 jar 文件ver15 ,然后将其添加到类路径中。 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所以问题是为什么我收到错误“java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver”并且我在依赖项选项卡中看不到这个 Z68995FCBF432492D15484D04A9D2AC40
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:尽管 jar 在我的build.gradle文件中的依赖项部分中作为编译时依赖项和测试时依赖项被提及:
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.看起来 IntelliJ 没有导入这些依赖项的添加。 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.您可以通过单击 IDEA 的 gradle 选项卡中的重新导入按钮重新导入您的 gradle 文件。 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".您可以通过单击 gradle 选项卡中的 Gradle 设置按钮启用自动导入,并启用“根据构建脚本文件的更改自动导入此项目”。
On a separate not, you don't need to declare testCompile
if you also declare a dependency as compile
.如果您还将依赖项声明为compile
,则无需声明testCompile
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.