2

我将带有属性的文件放入 *tomcat_folder*/conf 并尝试读取它:

InputStream input = this.getClass().getClassLoader().getResourceAsStream("conf.properties");
properties.load(input);

但实际上我收到'null'。我试图打印出当前文件夹,它显示“home/username”文件夹(我想从当前文件夹进入 conf 文件夹)。

“catalina.properties”包含“shared.loader=”,所以理论上这个文件应该从那里读取,但它不是......

有没有关于如何做到这一点的解决方案?

4

3 回答 3

3

我找到了解决方案。我将 ${catalina.home}/conf 添加到 catalina.properties -> shared.loader

shared.loader=${catalina.home}/conf

this.getClass().getClassLoader().getResourceAsStream("conf.properties");

开始正确打开。

于 2012-04-26T22:45:17.827 回答
0

我认为调用 this.getClass().getClassloader() 时获得的类加载器不是共享的。实际上,这取决于“this”在哪里。

我不确定您是否能够通过类加载器获取此文件,即使我认为当前的类加载器应该委托。您也许可以通过一些 tomcat api 获取共享类加载器。

您可以尝试 CATALINA_HOME/BASE 属性以通过文件系统访问文件。

于 2012-04-26T13:32:53.247 回答
0

尝试

properties.load(Thread.currentThread().getContextClassLoader().
        getResource("filename.properties").openStream());`

在 JBoss 6 上为我工作。

于 2012-04-26T13:35:38.847 回答