<分区>
我有一个 CouponSystem 项目,它有一个 config/cs.properties 文件。 此项目导出为 cs.jar 并添加到另一个导出 web-app csee.war 的项目
当 CouponSystem 运行时(作为 java 应用程序),它会加载正确读取 config/cs.properties 的 Utils 类。
当 csee 在服务器 (tomcat7) 上运行时,我找不到该文件。在创建一些方法来了解 tomcat 查找该文件的位置之后,我意识到它在 tomcat/bin 目录中查找 - 所以如果我将文件放在 tomcat/bin/cs.properties 中:一切正常。 (不是解决方案,只是为了理解问题)
我的问题是 - 将 cs.properties 放在网络应用程序环境中的正确方法在哪里,以及如何告诉非 servlet 类 (Utils) 找到它?
谢谢。
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);
}
}
}