DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Read A Properties File Inside Jar File

10.17.2005
| 48391 views |
  • submit to reddit
        
String sConfigFile = "config/mail.properties";
	
InputStream in = mailSender.class.getClassLoader().getResourceAsStream(sConfigFile);
if (in == null) {
	//File not found! (Manage the problem)
}
Properties props = new java.util.Properties();
props.load(in);
    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 2:15pm

Corrected. Thanks Llucifer

Snippets Manager replied on Mon, 2012/05/07 - 2:15pm

Beware that ClassLoader#getResourceAsStream returns null if the resource is not found. The will throw an ugly NPE at props.load(in).