| From | Sent On | Attachments |
|---|---|---|
| echa...@TXU.COM | Dec 4, 2002 1:35 pm | |
| Will Hartung | Dec 4, 2002 2:41 pm | |
| Roberto Bouza | Dec 4, 2002 2:48 pm | |
| Jay Wright | Dec 4, 2002 3:13 pm | |
| micael | Dec 4, 2002 3:25 pm | |
| Jay Wright | Dec 4, 2002 3:37 pm | |
| Will Hartung | Dec 4, 2002 3:46 pm | |
| Jacob Kjome | Dec 4, 2002 4:56 pm | |
| micael | Dec 4, 2002 5:12 pm | |
| Craig R. McClanahan | Dec 4, 2002 7:23 pm |
| Subject: | RE: Loading Properties Files | |
|---|---|---|
| From: | Jacob Kjome (ho...@visi.com) | |
| Date: | Dec 4, 2002 4:56:05 pm | |
| List: | org.apache.tomcat.users | |
No, that's not true at all.
The examples already given will find properties files for you just fine whether the file is in a directory structure or inside an archive. How do you think Java loads classes? It works out of archives, no?
here are some various was to access a properties file ( or any resource, for that matter) in whether the app is deployed as a directory or as a .war file (even inside a .jar file in WEB-INF/lib)....
This will load a file in WEB-INF/classes/conf or any jar file in the classpath with a package of "conf"... getClass().getResourceAsStream("/conf/db.properties");
This will load a file relative to the current class. For instance, if the class is "org.mypackage.MyClass", then the file would be loaded at "org.mypackage.conf.dbproperties". Note that this is because we didn't prepend "/" to the path. When that is done, the file is loaded from the root of the current classloader where this loads it relative to the current class... getClass().getResourceAsStream("conf/db.properties");
this will find db.properties anywhere in the current classloader as long as it exists in a "conf" package... getClass().getClassLoader().getResourceAsStream("conf/db.properties");
This will find the file in a "conf" directory inside the webapp (starting from the root). This starts looking in the same directory as contains WEB-INF. When I say "directory", I don't mean "filesystem". This could be in a .war file as well as in an actual directory on the filesystem... getServletContext().getResourceAsStream("/conf/db.properties");
of course you would probably not want just anyone seeing your db.properties file, so you'd probably want to put in inside WEB-INF of your webapp, so.... getServletContext().getResourceAsStream("/WEB-INF/conf/db.properties");
If your db.properties exists in another classloader which your app has
access to, you can reach it by using:
Thread.currentThread().getContextClassLoader().getResourceAsStream("conf/db.properties");
That will act similar to getClass().getClassLoader(), but it can see across all available classloaders where the latter can only see within the classloader that loaded the current class.
So, as you can see, you have quite a number of options. There you go.
Jake
At 03:37 PM 12/4/2002 -0800, you wrote:
If I understand you correctly, the properties file CANNOT be in the war file, it needs to be external. Right.
-----Original Message----- From: micael [mailto:cara...@harbornet.com] Sent: Wednesday, December 04, 2002 3:25 PM To: Tomcat Users List Subject: RE: Loading Properties Files
Depends upon what you want to do with the properties files and how you access them. Some ways of accessing them require that the name to access be relative to the classpath, others don't. You are better off to learn about properties files in this instance. There is nothing peculiar to the web-structure that I know of that is important about locating properties files.
At 03:14 PM 12/4/2002 -0800, you wrote:
And if you have a .war file? Then where would you put your properties files?
-----Original Message----- From: Roberto Bouza [mailto:rbo...@efcholdings.com] Sent: Wednesday, December 04, 2002 2:49 PM To: Tomcat Users List Subject: Re: Loading Properties Files
Thats right.
If you don't have a .war file, you can use the classes dir inside your WEB-INF dir, and create a new directory like "conf", the put inside all the properties files. In that way the ClassLoader looks for the files in there when you use something like this:
try { Properties props = new Properties(); InputStream in = getClass().getResourceAsStream("/conf/db.properties"); props.load(in); ......
propertie1 = props.getProperty("propertie1");
C'ya
Quoting Will Hartung <wil...@msoft.com>:
From: <echa...@TXU.COM> Subject: Loading Properties Files
My problem is that the class cannot location my properties file. I am unable to use other suggested methods that I have noticed on this list since those problems involved Properties File within Servlets.
After some testing, I determined for some reason the default directory it is looking for my properties file is the Windows System Directory
(Determined this
by
opening a file in the default directory, outputing something in it and searching for the file).
Anyone have any ideas on how to solve this problem? I do not want to hard code the exact location due to obvious reasons
The problem is that you appear to be loading a file with an absolute path, versus the common form of load a properties file via the ClassLoader.
Fumble about with the ClassLoader.getResourceAsStream to have it hunt down your properties file, and then feed that stream to your Properties.
public static yourMethod() { ClassLoader cl = YourClass.class.getClassLoader(); Properties prop = new Properties(); prop.load(cl.getResourceAsStream("yours.properties")); }
Then, just drop your properties at the right place in your WARs classes area.
Regards,
Will Hartung (wil...@msoft.com)
-- = Roberto Bouza Fraga = =================================== Research & Development Engineer Ella Cisneros Fontanals Holdings Ph: (305)-860-0116 / Fax:(305)-860-9401 =================================== e-Mail:rbo...@efcholdings.com
-- To unsubscribe, e-mail:
<mailto:tomc...@jakarta.apache.org> For additional commands, e-mail: <mailto:tomc...@jakarta.apache.org>
-- To unsubscribe, e-mail:
<mailto:tomc...@jakarta.apache.org>
For additional commands, e-mail:
<mailto:tomc...@jakarta.apache.org>
Micael
-------------------------------------------------------
This electronic mail transmission and any accompanying documents contain information belonging to the sender which may be confidential and legally privileged. This information is intended only for the use of the individual or entity to whom this electronic mail transmission was sent as indicated above. If you are not the intended recipient, any disclosure, copying, distribution, or action taken in reliance on the contents of the information contained in this transmission is strictly prohibited. If you have received this transmission in error, please delete the message. Thank you





