user234194
user234194

Reputation: 1723

issue loading properties file

I am having issue with loading

test.xml and test.properties 

inside the same folder conf.

I have a myProject.jar inside dist folder and test.xml and test.properties inside conf folder.

To load xml, I am using

document = reader.read(new File("../conf/test.xml"));//its working

But I am having issue when loading properties file, I am using

Class_name.class.getResourceAsStream("../conf/test.properties"),

   getResourceAsStream("conf/test.properties"),
   getResourceAsStream("/test.properties"),
   getResourceAsStream("test.properties"),

Nothing is working for properties file.

Any help is appreciated.

Upvotes: 2

Views: 611

Answers (3)

N.S.Karthik
N.S.Karthik

Reputation: 481

Remember the xml / properties ..what so ever file should be present root dir of the package

ex: com.abce.configuration --->readfile.java

xyz.xml com -->abcd -->configuration --> readfile.java

Upvotes: 1

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

It is important to differentiate between Class.getResourceAsStream(...) and ClassLoader.getResouceAsStream(...). The call from Class is a relative path. So I imagine the following would work:

Foo.class.getResourceAsStream("/conf/test.properties"),

... provided the parent of conf/ is in the classpath.

If this doesn't work, look into how the classloaders are setup in your environment. You need to do a lookup from classloader that includes the conf directory.

Upvotes: 2

RMT
RMT

Reputation: 7070

Why don't you take the file and load it using an FileInputStream

Properties properties = new Properties();
 properties.load(new FileInputStream(fileName));

The above code will take the properties file and load it into a properties object.

Upvotes: 6

Related Questions