Read properties file in a jar

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!
發佈了107 篇原創文章 · 獲贊 11 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章