4

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

user2210071
  • 203
  • 3
  • 6
  • 11

2 Answers2

3

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.

joan
  • 2,407
  • 4
  • 29
  • 35
  • if i placed the property file outside the web project for example in d drive..How can i include this path in the class path? – user2210071 Mar 31 '13 at 08:36
  • In this case you can use an absolute path: Properties properties = new Properties(); properties.load(new FileInputStream("D:/absolute/path/to/a.properties"); – joan Mar 31 '13 at 08:45
  • Sory..i dont want to hard code the path ,i just want to give the property file name only...To load it from the tomcat server where i can place it in tomcat? – user2210071 Mar 31 '13 at 13:24
2

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?

Community
  • 1
  • 1
Biswajit
  • 2,434
  • 2
  • 28
  • 35