我有一个 Maven 项目设置,例如
parent
...pom.xml
...servlet-app
......pom.xml ( specifies simple-lib as a dependency )
...simple-lib
......src/main/resources/config.properties
......src/main/java/package1/Config.java
......src/main/java/package1/HelloQuartzJob.java
基本上,servlet 应用程序 servlet-app 将 simple-lib 指定为依赖项。配置文件 config.properties 打包在 simple-lib.jar 的顶层/级别。当我解压servelet-app.war 时,我可以看到WEB_INF/lib/simple-lib.jar。所以,一切都很好。
Config.java 看起来像这样:
public class Config{
static final String PROPERTILES_FILE = "config.properties";
static Properties props;
static{
log.info("Loading Config properties from {}", PROPERTILES_FILE);
props = new Properties();
try {
InputStream is = ClassLoader.getSystemResourceAsStream(PROPERTILES_FILE);
if (is == null) {
log.info("Loading through ClassLoader as root");
is = ClassLoader.getSystemResourceAsStream("/"+PROPERTILES_FILE);
}
if (is == null) {
log.info("Loading through Config.class. root");
is = Config.class.getResourceAsStream("/"+PROPERTILES_FILE);
}
if (is == null) {
log.info("Loading through Config.class. relative");
is = Config.class.getResourceAsStream(PROPERTILES_FILE);
}
if( is == null ) {
log.info("Thread class loader root");
is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/"+PROPERTILES_FILE);
}
props.load(is);
} catch (Throwable e) {
log.error("Config properties loading error ", e);
throw new RuntimeException(e);
}
}
}
但是,当我在 tomcat7 中部署它时,我得到一个 NoClassDefFound/Could not initialize package1.Config 错误。我相信这是因为类加载器。
同样有趣的是,这个 package1.Config 被 HelloQuartzJob.java 使用,它是一个由在这个 servlet 应用程序中运行的调度程序实例运行的石英作业。
任何指针?