I use Spring boot maven plugin to package application as jar file.
It can find the resource file direct run in Itellij IDE, But it can not find resource file after, it display error as :
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/target/YYY.jar!/BOOT-INF/classes!/jmxremote.password
However, the file "jmxremote.password" indeed exist in the jar file.
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;
}