I’m making a REST API in Java using Spring Boot. And I’m encountering an issue that I don’t understand : when I run my code through vscode, everything works just fine. But when I try to run the compiled .jar file, I get the following error message :
<message>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class</message>
<frame>
<class>org.springframework.beans.factory.support.ConstructorResolver</class>
<method>createArgumentArray</method>
<line>798</line>
</frame>
It is a PostgrSQL database.
I guess that something is probably wrong with my pom.xml file ?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.languagehub</groupId>
<artifactId>languagehubpublicapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>languagehubpublicapi</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<scope>compile</scope>
</plugin>
</plugins>
</build>
</project>
And here’s my application.properties file :
spring.datasource.url = jdbc:postgresql://localhost:5432/languagehub
spring.datasource.username = [USER]
spring.datasource.password = [PASSWORD]
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=org.postgresql.Driver
server.port = 8080
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
Thank you for reading ! If you had any ideas, I’ll be really thankful for your help !
I wish you all an excellent day !
EDIT : I’ve modifier the part of postgresql dependency in the pom.xml file. I now have the following :
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
But I’m still getting the same error…
2
Answers
Your problem is right here:
When you say runtime, you’re telling maven that the jar will be provided at runtime (as in, deployment) and that it shouldn’t include this in the bundling. So when you create your boot jar this dependency isn’t getting included. remove
scope
or just added it to the CLI manually (not advised). The default scope iscompiled
VS code is bridging the gap for you here and putting it on the classpath when you run and you’re just not aware.
have You tried removing
<scope>runtime</scope>
from postgresql dependency?