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!