When I set the path of properties file, it's OK when we run in IDE. When I packed it in a jar and tried to run that, it won't pass. It seems strange because the path of the properties will be changed in the jar.
Then I found a way online, here it is:
public static String getPropertyValueByKey(String key) {
try {
ClassLoader myCL = MyUtil.class.getClassLoader();
errorProp.load(myCL.getResourceAsStream("errorMsg.properties"));
} catch (IOException ex) {
_log.error("Exception happens!" + ex.getMessage());
}
return errorProp.getProperty(key);
}MyUtil is the class which the method belongs to. Try it, it works!
在将properties文件打包成jar后遇到路径问题,通过使用ClassLoader类成功解决。

被折叠的 条评论
为什么被折叠?



