Syed
Syed

Reputation: 2607

Not able to load properties file in Dynamic web project in Eclipse for Java

I've put the properties file in Webcontent folder of my dynamic web project. But, it throws null pointer exception. Here is my code.

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream input = classLoader.getResourceAsStream("Resources.properties");
    System.out.println(input);
    Properties properties = new Properties();       
    properties.load(input);     
    System.out.println("MySQL Driver :"+properties.getProperty("mysqlDriver"));

My properties file include

mysqlDriver = com.mysql.jdbc.Driver

I'm getting error at InputStream input = classLoader.getResourceAsStream("Resources.properties");

The error is

Feb 09, 2016 2:58:50 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [DatabaseAccess] in context with path  [/iNTU] threw exception
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)

Upvotes: 0

Views: 1744

Answers (1)

ernest_k
ernest_k

Reputation: 45329

Put the "Resources.properties" file in the "src" folder of your dynamic web project (create it by right-clicking Jara Resources/src > new > general > File, named "MyResources.properties"). The code to read the properties file should then be updated to:

InputStream input = classLoader.getResourceAsStream("/Resources.properties");
//If you use a subfolder of src, please update accordingly.

With that, Eclipse will know how to deploy the compiled files to the war's WEB-INF/classes along with the properties file.

Upvotes: 3

Related Questions