gpt4 book ai didi

spring boot打包后找不到资源文件

转载 作者:行者123 更新时间:2023-12-05 02:14:09 24 4
gpt4 key购买 nike

我使用 Spring boot maven 插件将应用程序打包为 jar 文件。

可以找到直接在Itellij IDE中运行的资源文件,但是之后找不到资源文件,显示错误为:

java.io.FileNotFoundException: 类路径资源 [jmxremote.password] 无法解析为绝对文件路径,因为它不驻留在文件系统中:jar:file:/home/XXX/target/YYY.jar!/BOOT-INF/classes!/jmxremote.password

但是,jar文件中确实存在“jmxremote.password”文件。

    private Properties initialJMXServerProperties() throws RuntimeException {
URL passwordURL = JMXConfig.class.getClassLoader().getResource(passwordFileName);
URL accessURL = JMXConfig.class.getClassLoader().getResource(accessFileName);

String passFile = Optional.ofNullable(passwordURL).map(URL::getPath).orElseThrow(() -> new RuntimeException("JMX password file not exist"));
String accessFile = Optional.ofNullable(accessURL).map(URL::getPath).orElseThrow(() -> new RuntimeException("JMX access file not exist"));

Properties properties = new Properties();
properties.setProperty(PASSWORD_FILE_PROP, passFile);
properties.setProperty(ACCESS_FILE_PROP, accessFile);
return properties;
}

最佳答案

您不能从 JAR 加载文件作为 URL。您必须将其作为 InputStream 加载。

在你的情况下:

InputStream passwordInputStream = 
JMXConfig.class.getClassLoader().getResourceAsStream(passwordFileName);

在这里阅读更多: Reading a resource file from within jar

关于spring boot打包后找不到资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54053603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com