简体   繁体   English

Spring应用程序在intellij中运行,但java -jar不行

[英]Spring app run work in intellij, but java -jar doesnt

I create restfull spring app with mySQL database. 我用mySQL数据库创建了restfull spring app。 MyApp running well in Intellij, i build the jar with build artifacts in intellij. MyApp在Intellij中运行良好,我在intellij中构建了带有构建工件的jar。 When i run the jar (java -jar) i got error. 当我运行jar(java -jar)时,我收到了错误。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration'的bean时出错:通过构造函数参数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; 嵌套异常是org.springframework.beans.factory.BeanCreationException:在类路径资源中定义名为'dataSource'的bean时出错[org / springframework / boot / autoconfigure / jdbc / DataSourceConfiguration $ Hikari.class]:通过工厂方法进行Bean实例化失败; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: 嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.zaxxer.hikari.HikariDataSource]:

here is my application.properties 这是我的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 这是我的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 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 有人可以帮忙吗?谢谢

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. 你正在运行正确的jar - 你可能有多个 - 一个有app,一个来自boot在另一个dir。 Would be good to see a command you use to build as well. 很高兴看到你用来构建的命令。

使用mvn包构建命令,它将构建一个带有依赖项的jar并使用该jar

My jar running well, after i choose to manual config Database connection 我选择手动配置数据库连接后,我的jar运行良好

add in Application class 在Application类中添加

@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 我不知道为什么我的罐子不能从春天开始自动配置,也许有人可以在下面的评论中解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring boot 应用程序适用于 intellij 但不适用于 .jar 文件 - spring boot app works on intellij but not with .jar file Spring Boot+Java 9 Modules+Intellij Communicaty Edn:运行配置的行为不同于 jar 和 mvn spring-boot:运行 - Spring Boot+Java 9 Modules+Intellij Communicaty Edn: Run configuration behaves differently to jar and mvn spring-boot:run 为什么以 jar 运行时会出现 java.lang.ClassNotFoundException,但与 IntelliJ IDEA 配合良好 - Why get java.lang.ClassNotFoundException when run as jar, but work well with IntelliJ IDEA 使用“以Spring Boot应用程序身份运行”启动Apllication时,JUnitCore运行不起作用 - JUnitCore run doesnt work when the Apllication is started with “run as Spring Boot App” .jar不能在Debian上运行 - .jar doesnt run on debian Tomcat上的Spring应用程序在weblogic.jar中找不到类 - Spring app on Tomcat doesnt find class in weblogic.jar crontab对于运行Java类不起作用 - crontab doesnt work for run java class 如何在没有 IntelliJ 的情况下在本地主机上运行 Spring 应用程序? - How to run Spring app on localhost without IntelliJ? 如何在 intellij 中运行 Spring Boot 应用程序? - How to run a spring boot app in intellij? Ini4j.jar不适用于Java中的注册表文件 - Ini4j.jar doesnt work with registry file in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM