简体   繁体   中英

Spring Boot deployed JAR not finding properties file in deployed directory

I have tried a few suggestions to get this to work but it still fails. I have deployed a jar file through spring boot to a remote location and need the properties file external to it in order to be able to make config changes at any time.

When the properties file is specified as below, the jar reads the file but only the file that was included in the IDE in /resources before building the jar. When I change the format to (./application.properties) , the jar does not find any file.

  public static void main(String[] args)
             throws Exception
  {
    Properties prop = new Properties();
    try {
            InputStream s =  
                getClass().getClassLoader().getResourceAsStream("application.properties");
            if (s != null) prop.load(s);
            else {
                System.out.println("**NOT FOUND - configuration file:          
                **********************");LOGGER.info("Starting application.");
             ....

Tried running it like this as well to specify the location of the properties file to run:

java -jar -Dfile.encoding=utf-8 -jar -Dspring.config.location=/config/application.properties 
       searchapp.jar

Please any ideas or suggestions appreciated.

Change

java -jar -Dfile.encoding=utf-8 -jar -Dspring.config.location=/config/application.properties searchapp.jar

to

java -jar -Dfile.encoding=utf-8 -jar -Dspring.config.location=classpath:/config/application.properties searchapp.jar

or other way, by using absolute path (just for example, because I don't know your real absolute path)

java -jar -Dfile.encoding=utf-8 -jar -Dspring.config.location=file:./config/application.properties searchapp.jar

Reference document: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-application-property-files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM