인프런 커뮤니티 질문&답변

이승환님의 프로필 이미지

작성한 질문수

자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지]

38강. profile과 H2 DB

38강 Cannot load driver class: org.h2.driver

23.10.09 21:01 작성

·

1.7K

1

안녕하십니까. 다름이 아니라 profile을 local로 실행 시 오류가 나서 질문을 드립니다.

 

인텔리제이 Community 버전이라서

VM option 추가 후, -Dspring.profiles.active=local를 입력헀습니다.

 

실행을 할때 local 프로필이 활성화가 되었다고 하지만

 

오류가 발생합니다. VM option이 잘못된건가 싶어서

-Dspring.profiles.active=dev로 설정 시 MySQL이 정상적으로 작동합니다.

즉 h2관련 코드를 제가 잘못작성했거나 아니면 다른이유로 오류가 발생한다는건데 문제점을 제대로 파악을 하기가 힘들어서 질문을 드립니다.

 

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

Execution failed for task ':LibraryAppApplication.main()'.

> Process 'command 'C:/Program Files/Java/jdk-17/bin/java.exe'' finished with non-zero exit value 1

* Try:

> Run with --stacktrace option to get the stack trace.

> Run with --info or --debug option to get more log output.

> Run with --scan to get full insights.

 

답변 1

0

최태현님의 프로필 이미지
최태현
지식공유자

2023. 10. 10. 16:39

안녕하세요, 승환님! 🙂 질문 남겨주셔서 감사합니다 ㅎㅎㅎ

남겨주신 내용으로 제가 파악 가능한 부분은

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

에서 org.h2.driver 를 찾지 못하고 있다는 부분입니다!

 

build.gradle 에서 H2 드라이버가 설정되어 있는지, gradle refresh가 잘 되었는지 확인해보시면 좋을 것 같고, 혹시나 계속해서 잘 안된다면 (스프링 버전에 따라 자동으로 버전을 잡아주지 못할 수 있습니다)

runtimeOnly 'com.h2database:h2'

아래와 같이 명시적인 버전을 적어주시면 해결될 것 같습니다.

이러한 버전 목록은 https://mvnrepository.com/artifact/com.h2database/h2 에서 확인하실 수 있습니다.

runtimeOnly 'com.h2database:h2:2.2.224'

 

꼭 해결되셨으면 좋겠습니다. 감사합니다!! 😊