2

I have a project CouponSystem which have a config/cs.properties file. This project exported as cs.jar and added to another project which exporting a web-app csee.war

when the CouponSystem runs (as java application) it loads the Utils class that reads config/cs.properties correctly .

when the csee runs on server (tomcat7) i cannot find that file. after creating some method to understand where the tomcat looks for that file, i realize that its looking on the tomcat/bin directory - so if i put the file in tomcat/bin/cs.properties : everything works . ( not a solution, just to understand the problem)

my question is - where is the correct way to put the cs.properties in a web-app environment , and how to tell a non servlet class (Utils) to find it ?

Thanks .

enter image description here

public class Utils {

    public static final String CONFIG_FILE = "cs.properties";
    //public static final String CONFIG_FILE = "WEB-INF/config/cs.properties";

    // builds a hashmap from properties file
    public static void loadSystemParameters() {
        // this code only to know "the default current dir"
        File f = new File("."); // current directory
        File[] files = f.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.print("directory:");
            } else {
                System.out.print("     file:");
            }
            try {
                System.out.println(file.getCanonicalPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }




        Utils.logMessage(new Utils(), Severity.DEBUG, "loadSystemParameters() invoked");
        // construct a List<Customer> to return the data
        sysParams = new HashMap<String, String>();
        Properties properties = new Properties();
        try {
            properties.load(new FileReader(CONFIG_FILE));
            for(String propName : properties.stringPropertyNames()){
                sysParams.put(propName, properties.getProperty(propName));
            }
            Utils.logMessage(new Utils(), Severity.DEBUG, "properties from file loaded to a hashmap");
        } catch (IOException e) {
            Utils.logMessage(new Utils(), Severity.ERROR, "cannot load properties file ! exiting.");
            e.printStackTrace();
            //System.exit(0);
        }
    }
}
chenchuk
  • 5,324
  • 4
  • 34
  • 41

2 Answers2

0

The easiest way is to put the file in WEB-INF/classes and then load it from the classpath:

Properties p = new Properties();
// load the file at WEB-INF/class/cs.properties
p.load(Utils.class.getResourceAsStream('/cs.properties')); 
sh0rug0ru
  • 1,596
  • 9
  • 9
0

One way of achieving is to parameterize the path of directory containing your properties file. Suppose, you name is as CONFIG_HOME. You can set CONFIG_HOME as System property using JVM arguments. Then in your code, you can get CONFIG_HOME path as:

File f = new File(System.getProperty("CONFIF_HOME")); // your config directory

This approach gives you flexibility of not packaging the properties file inside your war.

Mohit
  • 1,740
  • 1
  • 15
  • 19