I am reading a properties file from the Tomcat\conf\somename.properties directory using
String demo = System.getProperty("catalina.base") +
File.separator + "conf" + File.separator + "somename.properties";
This is working perfectly fine with Tomcat. But, there can be scenario where client may use any other server like Glassfish or Websphere, in that case I won’t be able to get System.getProperty("catalina.base").
How should I solve it properly? I’m able to do that using ResourceBundle but for that I have to keep my properties file in my build, which I don’t want. I just want to read my properties file from outside my build.
There are basically two ways.
Just add its path to the runtime classpath so that you can get it from the classpath the usual way. In case of Tomcat, you can add external folders to the runtime classpath by specifying it in the
shared.loaderproperty of/conf/catalina.properties. E.g.shared.loader = ${catalina.home}/confOr better, don’t be server-specific
Other servers also supports adding external folders to the classpath, consult their documentation.
This way you’ll be able to get an
InputStreamof it from the classpath as follows:Add another server-independent system property yourself, you can set as a VM argument.
In case of Tomcat, you can set it as
JAVA_OPTSenvironment variable, or edit thecatalina.batstartup file or edit the Windows Service settings (when it’s installed as Windows Service), etc. Other servers supports similar constructs as well.This way you can obtain it as follows
Either way you choose, when distributing your application, you should document it properly so that the serveradmin can configure it accordingly.
Unrelated to the problem, the
ResourceBundleis not the right way to read configuration properties files. It’s intented for localized content for internationalization.