Dragon
Dragon

Reputation: 2481

Read conf.properties file from web app under Tomcat

I put a file with properties to *tomcat_folder*/conf and try to read it:

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

But in fact I receive 'null'. I tried to print out current folder and it shows "home/username" folder (I wanted to get to the conf folder from current one).

"catalina.properties" contains "shared.loader=", so theoretically this file should be read from there anyway, but it isn't...

Is there any solution on how to do this?

Upvotes: 2

Views: 5498

Answers (3)

Dragon
Dragon

Reputation: 2481

I found the solution. I added ${catalina.home}/conf to catalina.properties -> shared.loader

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

And

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

started to open correctly.

Upvotes: 3

Petr Mensik
Petr Mensik

Reputation: 27496

Try

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

Works for me on JBoss 6.

Upvotes: 0

Dario Pedol
Dario Pedol

Reputation: 2110

I don't think the classloader you get when calling this.getClass().getClassloader() is the shared one. Actually it depends on where "this" is.

I'm not sure you will be able to get this file via classloader, even though I think the current classloader should delegate. You can maybe get the shared classloader through some tomcat api.

You could try the CATALINA_HOME/BASE property to get to the file via filesystem.

Upvotes: 0

Related Questions