Spring Boot部署的JAR文件在部署目录中找不到属性文件。

huangapple go评论75阅读模式
英文:

Spring Boot deployed JAR not finding properties file in deployed directory

问题

我尝试了一些建议来使这个工作起来,但仍然失败。我通过Spring Boot将一个JAR文件部署到远程位置,并且需要将属性文件放在外部,以便随时能够进行配置更改。

当属性文件指定如下时,JAR文件会读取该文件,但只读取在构建JAR文件之前在/资源中IDE中包含的文件。当我将格式更改为*(./application.properties)*时,JAR文件找不到任何文件。

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.");
            // ...
        }
    // ...

我还尝试了以下方式来指定属性文件的位置运行:

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

如果有任何想法或建议,请提供。

英文:

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.

答案1

得分: 1

将代码中的部分内容翻译如下:

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

修改为

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

或者使用绝对路径的方式(仅作为示例,因为我不知道你的真实绝对路径)

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

参考文档:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-application-property-files

英文:

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

huangapple
  • 本文由 发表于 2020年9月30日 23:42:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64141130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定