java - Properties file not found - how to locate it as resource? -


i have properties file @ location (from netbeans project explorer)

-mytest     +web pages     +source packages     -test packages         -<default package>             +env.properties     <---- here         +com.mycomp.gts.test         +com.mycomp.gts.logintest         .....         .... 

now when trying find file using code

inputstream propertiesinputstream = getclass().getresourceasstream("env.properties"); env.load(propertiesinputstream); 

its throwing java.lang.nullpointerexception

you cannot use class reference load resource, because resource path not relative class. use class loader instead:

inputstream propertiesinputstream = getclass().getclassloader()         .getresourceasstream("env.properties"); env.load(propertiesinputstream); 

or alternatively, can use context class loader of current thread:

inputstream propertiesinputstream = thread.currentthread()     .getcontextclassloader().getresourceasstream("env.properties"); env.load(propertiesinputstream); 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -