- This topic has 2 replies, 2 voices, and was last updated 21 years, 2 months ago by johnsw.
-
AuthorPosts
-
johnswMemberI have a web project which relies on other classes (in my project’s source filder) which require configuration information stored in a properties files. One class that needs its own properties file is not a servlet, and therefore cannot locate the file using the servletContext.
To load the properties file, I can use either of the following code statements:
properties.load(getClass().getResourceAsStream("/myapp.properties"));
or
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("/myapp.properties"));
If I place the properties file in the source folder, then it gets copied to tomcat’s /WEB-INF/classes, and loads fine.
If I place the properties file in my project’s /WEB-INF/lib directory, then it gets copied to tomcat’s /WEB-INF/lib directory but fails to load.
Is it not the case that the classloader will try loading from either /WEB-INF/classes or /WEB-INF/lib?
I don’t really want the configuration information contained in the properties file to be in a jar file in the lib directiory? Since this file needs to be modified easily according to which app server it gets deployed too. I don’t want the properties file “bound” to the code, but easily modifiable for different server deployements/environments.
Is there some easy way of achieving this?
Thanks
John
Win2k Eclipse 2.1.1 MyEclipse 2.6.1 tomcat 5.0.3
Scott AndersonParticipantJohn,
Is it not the case that the classloader will try loading from either /WEB-INF/classes or /WEB-INF/lib?
The classloader should look in the classes directory for the property file or for the property file to be contained within an archive in the lib directory, but not by itself in the lib directory. It seems Tomcat’s behavior is correct. It loads in that it loads it properly from the classes directory.
I don’t really want the configuration information contained in the properties file to be in a jar file in the lib directiory?
Then leave the source of it in the source directory and read it out of the classes directory as you’re currently doing.
I don’t want the properties file “bound” to the code, but easily modifiable for different server deployements/environments.
I don’t know what you mean by “bound to the code”, but the properties file does have to be on the classpath when deployed if you’re going to read it using the classloader.
–Scott
MyEclipse Support
johnswMemberApologies my mistake – I forgot that the properties file needs to be included in an archive if you want to access it from (the jar file) in the /WEB-INF/lib directory.
It’s been a long couple of days!
-
AuthorPosts