views:

72

answers:

1

What is the best practice for configuring a java class library?

At my work, i am writing a class library. Some of the classes needs a configuration to work with our system setup. This configuration may change, and the distribution of the jar file takes a lot of time, as it is going through a lot of test layers.

How do i do the configuration part? Should i create a properties file, and where should i put it?

Is there a standard way to do this in tomcat?

+2  A: 

There is no standard way to do this. You have a few places to store your configuration files,

  1. CLASSPATH. This is available for all Java classes.
  2. WEB-INF of webapp. You can only do this if the JAR is running inside a container.
  3. External directory. If you don't want change your distribution when you modify the configuration file, you have to store the configuration outside of the WAR.

For my webapp, I always store the configuration in 2 places. The server logic is like this,

  1. On startup, it checks for a JVM parameter (Say my.config=/config/my.properties). If it's defined, read the properties from there.
  2. If the parameter is not defined, read the default one in WEB-INF.

Please make sure you log which version you are using.

ZZ Coder