Reading properties from tomcat Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Reading properties from tomcat without wasting too much if your time.

The question is published on by Tutorial Guruji team.

What is the best practice for reading config file(s) for your application. Which folders in tomcat server are “on the classpath”.

I tried placing my config file in the TOMCAT_HOMEconf but still I can’t read it from my servlet.

Tried using this (there is file in conf):


I don’t have much properties maybe 10-15 I figured this won’t be problem. I remember when I was using jboss this wasn’t issue as this much.

In my app I also specify DB connection spring in the context.xml can I specify my properties in there as well somehow? Or how this works with tomcat?

I’d like to keep my properties seperate from my war/unpacked war.


Reason for asking this is because I don’t know where my app will be deployed (at what location)


There are many different ways but it depends on your needs:

To load a property file from $TOMCAT_HOME/conf directory you will need to access it using a object since the class loader (as in this.getClass().getClassLoader().getResourceAsStream(...) is just able to load files (and classes) from your class path (under WEB-INF/classes, WEB-INF/lib or $TOMCAT_HOME/lib).

The easiest example to load a file from the Tomcat’s config directory would be:

File configDir = new File(System.getProperty("catalina.base"), "conf");
File configFile = new File(configDir, "");
InputStream stream = new FileInputStream(configFile);
Properties props = new Properties();

Notice that this approach will make your code dependent of Tomcat (the loading mechanism depends on the fact of a Tomcat’s system property being available). This is something that I wouldn’t recommend at all so if you move your property file to a folder inside your classpath then you should be able to load it the way you tried and your application could be deployed in any container.

And, you could configure your properties as JNDI resources but it would be too much hassle to access them.

We are here to answer your question about Reading properties from tomcat - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji