无法加载驱动程序类: org.h2.Driver in spring boot

Posted

技术标签:

【中文标题】无法加载驱动程序类: org.h2.Driver in spring boot【英文标题】:Cannot load driver class: org.h2.Driver in spring boot 【发布时间】:2020-12-08 18:05:02 【问题描述】:

我正在尝试使用 h2 数据库和微服务。但是当我运行它没有运行的应用程序时,它显示错误。 我有尤里卡服务器和产品微服务。在产品微服务中,我使用了 h2database。它显示错误。

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception 
     is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat

Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'h2Console' defined in class path resource 
[org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.class]: Bean instantiation via 
factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.springframework.boot.web.servlet.ServletRegistrationBean]: Factory method 
'h2Console' threw exception; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' 
defined in class path resource 
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean 
 instantiation via factory method failed; nested exception is 
 org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception 
is java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver  

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[org.springframework.boot.web.servlet.ServletRegistrationBean]: Factory method 'h2Console' threw 
exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error 
creating bean with name 'dataSource' defined in class path resource 

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'dataSource' defined in class path resource 
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean 
instantiation via factory method failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception 
is java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver  

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception 
is java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver 

Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver  

以下是我在 pom.xml 中的依赖项

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

下面是我的Product微服务的application.properties文件

server.port=8082

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
spring.datasource.name=SunHome

#H2 settings
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

spring.datasource.url=jdbc:h2:mem:testdb  
spring.datasource.driverClassName=org.h2.Driver  
spring.datasource.username=sa  
springspring.datasource.password=  
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect  

请帮我解决这个错误。

【问题讨论】:

【参考方案1】:

检查 applcation.properties 是否有“奇怪”字符 配置应该是对的。

【讨论】:

以上是关于无法加载驱动程序类: org.h2.Driver in spring boot的主要内容,如果未能解决你的问题,请参考以下文章

无法加载驱动程序类: org.h2.Driver in spring boot

无法加载驱动程序类:在 Spring Boot 应用程序中的 org.h2.Driver

无法解析类或包 H2

h2 数据库的 ClassNotFound 异常 class.forname("org.h2.Driver")

无法在自动混合模式下连接到 H2 数据库

Windows 上的 Squirrel SQL & H2 插件:ClassNotFoundException: org.h2.Driver