Geek
Geek

Reputation: 1344

properties file in Web project

I have all my programming code packaged in a jar file. This Jar file i will be placing in my dynamic web project to execute the logic in a jar.

But jar file has some logic which reads a .properties file to get some configuration values.

When i run my web project for the logic i get a error java.io.FileNotFoundException: conf\conf.properties (The system cannot find the path specified)

WebContent
|__WEB-INF
|___conf
|____conf.properties
|___lib
|_____myJar.jar
|__classes

I have following code for reading the properties file.

String propFileName = "conf/conf.properties"; 
Properties prop = new Properties();
prop.load(getClass().getResourceAsStream(propFileName));

Any suggestion to resolve this issue plz.

I have also tried with

String propFileName = "conf/conf.properties";
File file = new File(propFileName);
Properties prop = new Properties();
FileInputStream fileInput = new FileInputStream(file);
prop.load(fileInput);

Upvotes: 2

Views: 1455

Answers (2)

mtk
mtk

Reputation: 13709

To read a properties file web-inf folder in servlet project do the followoing

ServletContext.getResourceAsStream("WEB-INF/conf/conf.properties") 

Upvotes: 0

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

Your approach based on Class#getResourceAsStream(name) will only work if and only if your file is accessible from the ClassLoader of your calling class and here what you have directly under WEB-INF is not accessible, you should move conf/conf.properties in WEB-INF/classes instead and use /conf/conf.properties as resource name to make it get the file from the root not from the package of your calling class.

Upvotes: 2

Related Questions