【发布时间】:2019-05-31 21:23:03
【问题描述】:
我使用 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;
}
【问题讨论】: