Read external property file in java application

15,573

Solution 1

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.

Solution 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?

Share:
15,573
user2210071
Author by

user2210071

Updated on August 21, 2022

Comments

  • user2210071
    user2210071 almost 2 years

    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