Spring RESTful项目运行时SQL Server驱动异常排查求助
Hey there, let's work through this frustrating issue you're having. It's weird that everything works fine in Eclipse but breaks when you run the project outside of it—usually that points to a problem with how dependencies are being packaged or loaded at runtime. Here are the most common fixes to check:
Verify the driver is included in your build artifact
If you're using Maven to package your project (as a JAR or WAR), themssql-jdbcjar might not be getting bundled into the final file. Try these steps:- Run
mvn clean packageto build your project. - Unzip the resulting JAR/WAR file and look for the
libdirectory. Check ifmssql-jdbc-6.4.0.jre8.jaris present. - If it's missing, adjust your Maven configuration:
- For Spring Boot projects, ensure you're using the
spring-boot-maven-plugin(it should be included by default in initializer-generated projects), which creates a "fat jar" with all dependencies. - For standard WAR projects, confirm the
maven-war-pluginis configured to include compile-time dependencies (no extra exclusions that would remove the SQL driver).
- For Spring Boot projects, ensure you're using the
- Run
Check JRE compatibility
Your driver version6.4.0.jre8is built specifically for JRE 8. If you're running the project on a server with a newer JRE (like 11 or 17), you might hit compatibility issues. Either:- Upgrade the driver to a version matching your runtime JRE (e.g., use
9.4.0.jre11for JRE 11), or - Ensure your deployment environment uses JRE 8.
- Upgrade the driver to a version matching your runtime JRE (e.g., use
Confirm dependency scope is correct
Double-check your pom.xml to make sure themssql-jdbcdependency doesn't have ascopeset toprovidedortest. Those scopes tell Maven not to include the jar in the final build. Your dependency should look like this:<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc --> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>6.4.0.jre8</version> <!-- No scope attribute here, default is "compile" which is correct --> </dependency>Refresh your Maven local repository
Sometimes cached dependencies in your local Maven repo can get corrupted. Delete the directory~/.m2/repository/com/microsoft/sqlserver/mssql-jdbc/6.4.0.jre8(on Windows it'sC:\Users\<your-user>\.m2\repository\com\microsoft\sqlserver\mssql-jdbc\6.4.0.jre8), then runmvn clean installto re-download the driver jar.Check runtime classpath for manual deployments
If you're deploying to an application server like Tomcat:- Make sure the
mssql-jdbcjar is placed in your project'sWEB-INF/libdirectory (not just in Eclipse's build path). - Avoid placing it in the server's global
libdirectory unless necessary—this can cause conflicts with other apps.
- Make sure the
The error message "SQL Server jar file is not running" is a bit unusual; typically you'd see a "ClassNotFoundException" for the SQL driver. But the root cause almost always boils down to the driver jar not being available on the runtime classpath. Start with the first check (packaging the dependency) since that's the most common culprit when Eclipse works but the deployed app doesn't.
内容的提问来源于stack exchange,提问作者Shajitha Begum K

