properties file in Web project

时间:2016-10-20 18:49:23

标签: java java-ee web properties

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

2 个答案:

答案 0 :(得分:2)

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.

答案 1 :(得分:0)

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

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