4

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
David G
  • 153
  • 1
  • 1
  • 5
  • How did you include the SQL Server driver in your project? Is this dependency declared in Maven, or did you manually add it to the Eclipse buildpath? – Mark Rotteveel Jun 24 '13 at 14:39
  • I manually added it to the Eclipse buildpath. Should this be added as a dependency as well? – David G Jun 24 '13 at 14:41

1 Answers1

7

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:

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197