简体   繁体   English

Spring boot fat jar 无法从 application.properties 中定义的绝对路径读取文件

[英]Spring boot fat jar cannot read file from absolute path defined in application.properties

So i have a Spring boot fat jar that has a config that needs to load a .pem file from an absolute path defined in application.properties file.所以我有一个 Spring boot fat jar,它有一个配置,需要从application.properties文件中定义的绝对路径加载.pem文件。

I defined the path like this:我定义了这样的路径:

security.jwt.public-key=/opt/samara/staging.pem

The usage is like this:用法是这样的:

    @Value("\${security.jwt.public-key}")
    lateinit var publicKey: Resource

Here is the directory structure:这是目录结构:

root@samara:/opt/samara# ls -lsh
-rwxr-xr-x 1 root root  44M Nov 27 07:50 app.jar
-rwxr-xr-x 1 root root 2.0K Nov 27 07:53 staging.pem
-rwxr-xr-x 1 root root 1.2K Nov 27 08:29 application.properties

But when i tried to run it like this:但是当我尝试像这样运行它时:

root@samara:/opt/samara# java -jar app.jar

it failed with this:它失败了:

Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/opt/samara/staging.pem]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:158) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]

Actually, i copied those files from another machine and it runs perfectly in that machine but i don't know why it failed in the new machine.实际上,我从另一台机器复制了这些文件,它在该机器上运行良好,但我不知道为什么它在新机器上失败。 Thanks.谢谢。

After reading this: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#resources阅读本文后: https : //docs.spring.io/spring/docs/current/spring-framework-reference/core.html#resources

I changed the path definition to:我将路径定义更改为:

security.jwt.public-key=file:/opt/samara/staging.pem

And it worked!它奏效了!

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

相关问题 春季启动:从“ application.properties”读取文件路径 - Spring boot: Read a file PATH from “application.properties” Spring Boot - 从依赖的 jar 加载 application.properties/yml - Spring Boot - Load application.properties/yml from dependent jar 在 Spring Boot 中访问 application.properties 文件中定义的值时出现问题 - problem access a value defined in the application.properties file in Spring Boot 如何在 Spring Boot 中访问 application.properties 文件中定义的值 - How to access a value defined in the application.properties file in Spring Boot Spring Boot 获取 application.properties 的实例(或其文件路径) - Spring Boot get istance of application.properties (or its file path) Spring Boot集成测试无法访问application.properties文件 - Spring Boot integration tests cannot reach application.properties file 如何从 spring 引导应用程序之外的位置读取 application.properties 文件 - How to read the application.properties file from a location outside the spring boot application 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? 无法在Spring Boot服务中从application.properties加载值 - Cannot load values from application.properties in a spring boot service Spring Boot不会从application.properties文件中读取默认值 - Spring Boot doesn't read the default value from the application.properties file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM