user2210071
user2210071

Reputation: 203

Read external property file in java application

I want to load a property file from outside the web application.The code to get property file is shown below.

 Properties p = new Properties();
 p.load(this.getClass().getClassLoader().getResourceAsStream("a.properties"));

I’m using tomcat server and I want to place the property file inside the tomcat server.Where I can place it in the server inorder to available it in the class path while running the application? I don’t want to change the above code because I have to run the same application indifferent server also

Upvotes: 4

Views: 15902

Answers (2)

joan
joan

Reputation: 2551

I recommend the first option. Put the a.properties in the classpath. Then load with:

Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("a.properties"));

This way you can load properties relative tot the "root" of the classpath. Tt's recommended to use the ClassLoader as returned by Thread.currentThread().getContextClassLoader() for this.

Upvotes: 3

Biswajit
Biswajit

Reputation: 2516

There are basically three ways: The remaining portion you can see in Where to place and how to read configuration resource files in servlet based application?

Upvotes: 2

Related Questions