[Solved] Runnable jar unable to load application.properties file?


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:

enter image description here

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

enter image description here

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.

solved Runnable jar unable to load application.properties file?