可运行的jar无法加载application.properties文件? [英] Runnable jar unable to load application.properties file?

查看:85
本文介绍了可运行的jar无法加载application.properties文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Eclipse(不是STS-Eclipse)上使用SpringBoot开发了一个应用程序.项目结构如下:

虽然应用程序在Eclipse中运行得非常好,但却在制作可执行的jar(例如MyApp.jar)时无法运行.在我看来,可执行文件无法加载 application.properties 文件,或者它无法解析所需的属性字段值.我使用调试模式运行了该应用程序,但找不到有关缺少 application.properties 文件的任何线索.它包含的全部是 NumberFormatException .

登录错误详细信息:-

  [错误] 2017-08-04 12:03:36.301 [main] SpringApplication-应用程序启动失败org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为"myBusiness"的bean时出错:未通过fiel表示依赖关系d'myCommonProperty';嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为"myCommonConfig"的bean时出错d在URL [jar:file:/app/Keygen/bin/MyApp.jar!/com/keygen/config/MyCommonConfig.class]中:通过构造函数的Bean实例化失败;巢状d异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.keygen.config.MyCommonConfig $$ EnhancerBySpringCGLIB $$ f47f0d]:构造函数抛出异常;嵌套的异常是java.lang.NumberFormatException:对于输入字符串:"$ {thread.core.pool.size}"在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)〜[MyApp.jar :?] 

MyCommonConfig.java

  @Configuration公共类MyCommonConfig {...@Autowiredpublic MyCommonConfig(@Value("$ {thread.core.pool.size}")字符串corePoolSize,@ Value("$ {thread.max.pool.size}")字符串maxPoolSize,@ Value("$ {sys.blacklisted.keywords})字符串blackListedKeywords,@ Value(" $ {sys.sleep.time}))字符串sleepTime,@ Value(" $ {sys.keyword.size})字符串keywordSize,@ Value(" $ {sys.queue.size})字符串queueSize){this.corePoolSize = Integer.parseInt(corePoolSize);//获取数字格式异常,因为解析的值为"$ {thread.core.pool.size}",应为"500"this.maxPoolSize = Integer.parseInt(maxPoolSize);this.blackListedKeywords = blackListedKeywords;this.sleepTime = Integer.parseInt(sleepTime);this.keywordSize = Integer.parseInt(keywordSize);this.queueSize = Long.parseLong(queueSize);LOGGER.info(正在完成MyCommonConfig的加载");}...} 

下面是我的SpringBoot应用程序类

  @SpringBootApplication公共类MyApp {公共静态void main(String [] args){SpringApplication springApplication =新的SpringApplication(MyApp.class);springApplication.addListeners(new ApplicationPidFileWriter());springApplication.setRegisterShutdownHook(true);ApplicationContext applicationContext = springApplication.run(args);MyAppBusiness myAppBusiness = applicationContext.getBean(MyAppBusiness.class);myAppBusiness.serve();}} 

我正在执行脚本 ./keygen.sh ,其中包含以下命令:

  java -jar MyApp.jar 

二进制结构如下:

谢谢.

更新

请注意,我没有使用任何构建工具或STS-Eclipse来制作可执行的可运行jar.

更新2

添加jar文件详细信息

解决方案

最后得到了解决方案.在这里,我向您简要介绍您将遇到此类问题的情况:

1)您正在开发不使用STS-Eclipse IDE的Spring Boot应用程序.

2)您没有使用Maven/Gradle之类的构建工具来构建jar文件.

3)您只是使用传统Eclipse IDE的帮助制作了一个简单的可运行jar.

解决方案:-

1)如果以上情况都匹配,那么让我提醒您,您正在挑战开发Spring Boot应用程序的推荐方法.

2)因此,您必须采取与@Abdullah Khan在

5)现在,您可以准备制作jar文件并在具有以下结构的任何环境中运行:

注意:-您可能找不到任何预期的消息,例如缺少application.properties" 未找到application.properties的报告" 消息也是如此.

所以绝对不是重复的问题.

I developed an application using SpringBoot on Eclipse(not STS-Eclipse). The project structure looks as below:

While the application running absolutely fine in Eclipse but unable to run when making an excecutable jar (lets say MyApp.jar). It seems to me that the executable is unable to load the application.properties file or it is unable to parse the required property field value. I ran the application using debug mode but unable to find any clue regarding missing of application.properties file. All it contains is NumberFormatException.

Log Error details:-

[ERROR] 2017-08-04 12:03:36.301 [main] SpringApplication - Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBusiness': Unsatisfied dependency expressed through fiel
d 'myCommonProperty'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myCommonConfig' define
d in URL [jar:file:/app/Keygen/bin/MyApp.jar!/com/keygen/config/MyCommonConfig.class]: Bean instantiation via constructor failed; neste
d exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.keygen.config.MyCommonConfig$$EnhancerBySpringCGLI
B$$f47f0d]: Constructor threw exception; nested exception is java.lang.NumberFormatException: For input string: "${thread.core.pool.size}"
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor
.java:588) ~[MyApp.jar:?]

MyCommonConfig.java

@Configuration
public class MyCommonConfig {
       .
       .
       .
    @Autowired
    public MyCommonConfig(@Value("${thread.core.pool.size}")String corePoolSize,@Value("${thread.max.pool.size}")String maxPoolSize,@Value("${sys.blacklisted.keywords}")String blackListedKeywords,@Value("${sys.sleep.time}")String sleepTime,@Value("${sys.keyword.size}")String keywordSize,@Value("${sys.queue.size}")String queueSize) {
        this.corePoolSize=Integer.parseInt(corePoolSize);//Getting Number format exception as the value parsed is "${thread.core.pool.size}" which should be "500"
        this.maxPoolSize=Integer.parseInt(maxPoolSize);
        this.blackListedKeywords=blackListedKeywords;
        this.sleepTime=Integer.parseInt(sleepTime);
        this.keywordSize=Integer.parseInt(keywordSize);
        this.queueSize=Long.parseLong(queueSize);
        LOGGER.info("Loading MyCommonConfig complete");
    }
        .
        .
        .
}

Below is my SpringBoot application class

@SpringBootApplication
public class MyApp{
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(MyApp.class);
        springApplication.addListeners(new ApplicationPidFileWriter());
        springApplication.setRegisterShutdownHook(true);
        ApplicationContext applicationContext = springApplication.run(args);
        MyAppBusiness myAppBusiness = applicationContext.getBean(MyAppBusiness.class);
        myAppBusiness.serve();
    }
}

I am executing the script ./keygen.sh which contains the following command:

java -jar MyApp.jar

The binary structure looks as below:

Thanks in advance.

Update

Please note I am not using any building tool or STS-Eclipse to make execuatble runnable jar.

Update 2

Adding jar file details

解决方案

Finally got the solution. Here I am briefing the case scenario wherein you will face such problem:

1) You're developing a Spring Boot application without using STS-Eclipse IDE.

2) You're not using any bulding tool like Maven/Gradle to build the jar file.

3) You're just making simple runnable jar with the hepl of traditional Eclipse IDE.

Solution:-

1) If above cases matches, then let me remind you that you're challenging the recommended way of developing a Spring Boot application.

2) So you've to do exactly opposite what @Abdullah Khan Suggesting in comment. Means you've to remove the spring-boot-maven-plugin-1.5.4.RELEASE.jar if it is there, cuz it's not a maven project.

3) Even if the application.properties file loading implicitly and running fine without any problem in Eclipse IDE, that does not mean that your build jar will run. So you've to explicitly provide the location of your property file as follow:

@SpringBootApplication
@PropertySource("file:application.properties")//See here
public class MyApp{
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(MyApp.class);
        springApplication.addListeners(new ApplicationPidFileWriter());
        springApplication.setRegisterShutdownHook(true);
        ApplicationContext applicationContext = springApplication.run(args);
        MyAppBusiness myAppBusiness = applicationContext.getBean(MyAppBusiness.class);
        myAppBusiness.serve();
    }
}

4) Now change your project structure as follow:

5) So now you're ready to make your jar file and run in any environment with following stucture:

Note:-You might not able find any expected message like "missing application.properties" or "report of application.properties not found" message in log file even if you're running it in root debug mode.

So definietely this is not a duplicate question.

这篇关于可运行的jar无法加载application.properties文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆