java JAR 无法加载 com.microsoft.sqlserver.jdbc.sqlserverdriver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17277628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:34:01  来源:igfitidea点击:

JAR fails to load com.microsoft.sqlserver.jdbc.sqlserverdriver

javasql-server-2008jdbcclassnotfoundexception

提问by David G

There seems to be a number of similar questions related to this, but none have been able to provide me with any help. I'm running Microsoft's JDBC driveron SQL Server(I am using sqljdbc4.jar) and using integrated authentication to access my database. The code snippet for connecting is as follows:

似乎有许多与此相关的类似问题,但没有一个能够为我提供任何帮助。我在SQL Server上运行Microsoft 的 JDBC 驱动程序(我正在使用)并使用集成身份验证来访问我的数据库。连接的代码片段如下:sqljdbc4.jar

String connectionUrl="jdbc:sqlserver://servername:1433;integratedSecurity=true;";

try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    con = DriverManager.getConnection(connectionUrl);
}//catch, etc...

When I run the project in Eclipse, it starts up without a hitch. When I run Mavenclean install and pack it into a .jar, however, I get the error:

当我在Eclipse 中运行该项目时,它可以顺利启动。但是,当我运行Maven全新安装并将其打包到 .jar 中时,出现错误:

java.lang.ClassNotFoundException: Failure to load: com.microsoft.sqlserver.jdbc.SQLServerDriver
    at launch.JarClassLoader.loadClass(JarClassLoader.java:964)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at ui.SearchWindow.run(SearchWindow.java:97)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access0(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

I have attempted the solutions posted in other threads; I have a System CLASSPATHvariable that directs to the .jarand it is located in my build path and my runtime classpath. Perhaps the problem is staring me in the face. My best guess is that it is related to Maven, but how should I go about solving this?

我尝试了其他线程中发布的解决方案;我有一个CLASSPATH指向的系统变量,.jar它位于我的构建路径和运行时类路径中。也许问题正盯着我的脸。我最好的猜测是它与Maven有关,但是我应该如何解决这个问题?

Also, please let me know if I need to clarify any points; I'd be more than happy to do so.

另外,如果我需要澄清任何要点,请告诉我;我很乐意这样做。

回答by Mark Rotteveel

In your comment you confirm that you manually added it to the build path and not to the maven POM. You really need to add a dependency, otherwise Maven won't know about it when building.

在您的评论中,您确认您手动将其添加到构建路径而不是 maven POM。您确实需要添加一个依赖项,否则 Maven 在构建时不会知道它。

And add the dependency to the POM:

并将依赖项添加到 POM:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>8.1.1.jre8</version>
</dependency>

See also:

也可以看看: