Search code examples
spring-bootintellij-idea

ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver


I follow the sample in

https://spring.io/guides/gs/accessing-data-mysql/

it run successfully, then I try to change it to connect to my local sql server, so I add

<dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>
    </dependency>

in pom.xml, and in application.properties, I change it to

spring.jpa.hibernate.ddl-auto=update 
spring.datasource.url=jdbc:sqlserver://localhost:1433;encrypt=true;trustServerCertificate=true;databaseName=db_example
spring.datasource.username=sa
spring.datasource.password=p@ssw0rd
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.SQLServerDialect

and then in my run configuration, I kept it unchange, that is,

java 17, -cp accessing-data-mysql-complete

com.example.accessingdatamysql.AccessingDataMysqlApplication run configuration

then I run this configuration, but now error:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:467) at org.springframework.util.ClassUtils.forName(ClassUtils.java:283) at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.driverClassIsLoadable(DataSourceProperties.java:190) at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:171) at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:123) at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:48) at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:90) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) .......

is shown, why? the same run configuration is success to run the mysql sample.

  1. and in intellij console, I cannot see what classpath it is using, any method to show the classpath this run configuration is using?

  2. in intellij project settings > Modules > Dependencies, I just see Maven: com.mysql:mysql-connector-j:8.0.31 but no mssql-jdbc although my pom.xml has it, why? enter image description here


Solution

  • java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver at

    The SQL server JDBC library is still hasn't added to your classpath for some reason.

    Make sure that after adding the mssql-jdbc in pom.xml, you click the button in IDEA's top right to load the pom.xml. See here for details: https://www.jetbrains.com/help/idea/delegate-build-and-run-actions-to-maven.html#maven_reimport

    Also, I noticed that you have two modules. Check if you have added the SQL server JDBC library in the correct module.

    After succeeding in adding it, the IDEA should be able to show it in Settings > Modules > Dependencies.

    and in intellij console, I cannot see what classpath it is using, any method to show the classpath this run configuration is using?

    When running, the IDEA will show the full command for running this program in the Run tool window's first line1. You could copy it, and check the classpath there.