Geek Geek - 1 month ago 15
Java Question

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


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);

Answer

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.