atom feed10 messages in org.apache.tomcat.usersRE: Loading Properties Files
FromSent OnAttachments
echa...@TXU.COMDec 4, 2002 1:35 pm 
Will HartungDec 4, 2002 2:41 pm 
Roberto BouzaDec 4, 2002 2:48 pm 
Jay WrightDec 4, 2002 3:13 pm 
micaelDec 4, 2002 3:25 pm 
Jay WrightDec 4, 2002 3:37 pm 
Will HartungDec 4, 2002 3:46 pm 
Jacob KjomeDec 4, 2002 4:56 pm 
micaelDec 4, 2002 5:12 pm 
Craig R. McClanahanDec 4, 2002 7:23 pm 
Subject:RE: Loading Properties Files
From:micael (cara@harbornet.com)
Date:Dec 4, 2002 5:12:54 pm
List:org.apache.tomcat.users

A .war file is just a wrapper for a web application. I think he does not know what one is. It is like a .zip or a .jar file. Do you know that?

At 06:56 PM 12/4/2002 -0600, you wrote:

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,

<mailto:tomc@jakarta.apache.org> For additional commands, e-mail: <mailto:tomc@jakarta.apache.org>

<mailto:tomc@jakarta.apache.org>

For additional commands, e-mail:

<mailto:tomc@jakarta.apache.org>

Micael

-------------------------------------------------------

Micael

-------------------------------------------------------