0
votes

I create restfull spring app with mySQL database. MyApp running well in Intellij, i build the jar with build artifacts in intellij. When i run the jar (java -jar) i got error.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; 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]:

here is my application.properties

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/TestApp?useSSL=false
spring.datasource.username=root
spring.datasource.password=password

here is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.example MyApp 1.0-SNAPSHOT jar

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>fluent-hc</artifactId>
        <version>4.5.8</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Can anyone help?Thank you

3
How you have built a jar? Mvn build command?sankar
@sankar build artifacts in intellijwanz
"mvn clean build" try this on your terminal window. it will build a jar with dependencies and use that jarsankar
mvn package is right command for spring boot try that on your terminalsankar
@MehdiBenmesssaoud i have tried. It run wellwanz

3 Answers

0
votes

That would suggest you don't have all you dependencies in you jar. Are you running correct jar - you might have more than one - one with app and one from boot in a different dir. Would be good to see a command you use to build as well.

0
votes

My jar running well, after i choose to manual config Database connection

add in Application class

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

and then create

@Configuration
@PropertySource({ "classpath:app.datasource.properties" })
class DatabaseConfig {
@Autowired
private Environment env;

@Bean
@Primary
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getProperty("app.datasource.driverClassName"));
    dataSource.setUrl(env.getProperty("app.datasource.url"));
    dataSource.setUsername(env.getProperty("app.datasource.username"));
    dataSource.setPassword(env.getProperty("app.datasource.password"));

    return dataSource;
}    

i don't know why my jar can't get autoconfiguration from spring, maybe someone can explain in comments below

-1
votes

Use mvn package build commands it will build a jar with dependencies and use that jar