Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 driver on SQL Server (I am using sqljdbc4.jar) and using integrated authentication to access my database. The code snippet for connecting is as follows:

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 Maven clean install and pack it into a .jar, however, I get the error:

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$1.run(SearchWindow.java:97)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.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 CLASSPATH variable that directs to the .jar and 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?

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

like image 711
David G Avatar asked Mar 24 '23 10:03

David G


1 Answers

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.

And add the dependency to the POM:

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

See also:

  • https://github.com/Microsoft/mssql-jdbc
like image 67
Mark Rotteveel Avatar answered Apr 25 '23 19:04

Mark Rotteveel