环境:Ubuntu 18 + Spring boot 2.0 + JDK 11
应用程序在IntelliJ中运行时正确执行,但在Spring boot maven插件打包文件后读取文件时不正确。
PS:在打包的jar文件中确实可以找到该文件!
java.io.FileNotFoundException: class path resource [jmxremote.password]
cannot be resolved to absolute file path because it does not reside in the
file system: jar:file:/home/XXX/YYY/target/YYY-1.0-Final.jar!/BOOT-
INF/classes!/jmxremote.password
最佳答案
您很可能正在尝试使用 java.io
类来加载类路径资源。这行不通,java.io
类专为文件系统操作而设计。
从 JAR 外部提取 jmxremote.password
或使用常规 InputStream
将其作为类路径资源访问,例如通过使用 Class.getResourceAsStream()
:
try (InputStream in : getClass().getResourceAsStream("/jmxremote.password")) {
// read input
}
关于java - 无法读取spring boot打包jar文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54053817/