Error for Importing JDBC from sqlServer with Maven

Asked

Viewed 154 times

3

In my java web application ,I needed to add a connection to a sql server database, so I put it as a dependency on Maven as described below:

<dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>6.4.0.jre8</version>
        <scope>compile</scope>
    </dependency>

The dependency has been downloaded and is inside the folder m2.

Running the application locally on my machine, everything works with the import code below:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.microsoft.sqlserver.jdbc.SQLServerDriver;

However, after generating the WAR of the project with the mvn clean package -DskipTests -Pdev and posted within a development server, I have the following error:

Root cause of Servletexception. java.lang.Noclassdeffounderror: with/microsoft/sqlserver/jdbc/Sqlserverdriver

I believe, either I’m importing wrong in the code or I forgot to set something up for Maven.

I also checked that after running the Maven command the dependency is placed inside WEB-INF\lib as the other dependencies, but my application does not find, someone can help me?

  • On this development server, isn’t there another JAR added with the same name in an older version? Using Tomcat?

  • To run local I use Tomcat, but on the development server I published is Weblogic

  • Check if you do not have in this Weblogic a driver already added with the same name. It may be that two Jars with the same name exist in different versions, then in the classpath the first of them is considered and, perhaps, is occurring to find the old one before. This package has changed from one version to another, so I think it’s a possibility.

  • I got it, I’ll check. Another question, these JARS after build by Maven go to the WEB-INF/lib folder and application will "read" this folder or the server JARS folder?

  • Of the two. Depending on the application server, you even have the option to choose between trying to find the dependencies first on the server itself and then inside the application (and vice versa), to avoid this type of problem that I mentioned earlier. I believe that Weblogic has this control, including (if you have), you can try to change to your deploy this, but I can’t tell you how pq do not know Weblogic

  • But if it were inside the application the import path would be the same? " com.microsoft....."

Show 2 more comments

1 answer

0

I went through this same problem in the past and solved it in the following way:

Add the code below to your connection code:

static {
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    } catch (ClassNotFoundException e) {
        System.err.println("SQLServer DataSource unable to load SQLServer Driver");
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.