RE: properties files and tomcat
The problem is that java.util.Properties is loaded by the system class loader, which knows nothing about the class loaders for the web-applications. You need to do something like: public MyClass { public loadProps() { MyClass.class.getResourceAsStream("my.props"); } } Then, as long as MyClass and my.props are loaded by the same class loader, the properties file will be found and loaded. Randy -Original Message- From: Christoph Rooms [mailto:[EMAIL PROTECTED]] Sent: Tuesday, January 23, 2001 2:24 PM To: [EMAIL PROTECTED] Subject: properties files and tomcat Hi, I want to read a properties file like this : Properties.class.getResourceAsStream("my.properties"); This function should find the properties files when it's in the CLASSPATH. I have putted the properties file in the WEB-APPS dir ... anyone ? thanks ...Christoph - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]
RE: properties files and tomcat
Actually, there are good reasons to NOT do it that way. You may not have access to the file system for one, or the property file may be embedded ina JAR file. Whereas if you use the getResourceAsStream you don't have thise problems. However, if you need to access an arbitrary file, using FileInputStream is the way to go. For general purpose properties file usage, I reccommend you stay with getResourceAsStream. It makes it easy to switch the properties files you use by just changing the classpath a little bit. And it allows you to have defaults buried deep in the class path which are overridden by the same named resource which occurs earlier in the classpath. Just my 2 cents. Regards, Paul -Original Message- From: Greg Schueler [mailto:[EMAIL PROTECTED]] Sent: Tuesday, January 23, 2001 3:18 PM To: [EMAIL PROTECTED] Subject: RE: properties files and tomcat You should do this: Properties myprops = new Properties(); myprops.load(new FileInputStream(new File("my.properties"))); if this doesn't seem to work, make sure that my.properites is in the right place, and/or qualify the new File() call. e.g. new File("d:\tomcat\webapps\myapp\my.properties"); The CLASSPATH is only for loading class files, and will not help you find the properties file. -Original Message- From: Christoph Rooms [mailto:[EMAIL PROTECTED]] Sent: Tuesday, January 23, 2001 11:24 AM To: [EMAIL PROTECTED] Subject: properties files and tomcat Hi, I want to read a properties file like this : Properties.class.getResourceAsStream("my.properties"); This function should find the properties files when it's in the CLASSPATH. I have putted the properties file in the WEB-APPS dir ... anyone ? thanks ...Christoph - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]
RE: properties files and tomcat
Hi Greg, You can use the CLASSPATH to load properties files ... Properties p = new Properties(); java.io.InputStream is; is = getClass().getResourceAsStream("rnb.properties"); if (is == null) throw new Exception("Properties file not found"); p.load(is); This works fine ! greetz, christoph -Original Message- From: Greg Schueler [mailto:[EMAIL PROTECTED]] Sent: dinsdag 23 januari 2001 21:18 To: [EMAIL PROTECTED] Subject: RE: properties files and tomcat You should do this: Properties myprops = new Properties(); myprops.load(new FileInputStream(new File("my.properties"))); if this doesn't seem to work, make sure that my.properites is in the right place, and/or qualify the new File() call. e.g. new File("d:\tomcat\webapps\myapp\my.properties"); The CLASSPATH is only for loading class files, and will not help you find the properties file. -Original Message- From: Christoph Rooms [mailto:[EMAIL PROTECTED]] Sent: Tuesday, January 23, 2001 11:24 AM To: [EMAIL PROTECTED] Subject: properties files and tomcat Hi, I want to read a properties file like this : Properties.class.getResourceAsStream("my.properties"); This function should find the properties files when it's in the CLASSPATH. I have putted the properties file in the WEB-APPS dir ... anyone ? thanks ...Christoph - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]
Re: properties files and tomcat
Christoph Rooms wrote: Hi Greg, You can use the CLASSPATH to load properties files ... Properties p = new Properties(); java.io.InputStream is; is = getClass().getResourceAsStream("rnb.properties"); if (is == null) throw new Exception("Properties file not found"); p.load(is); This works fine ! This approach works fine, and can be used to load properties files that are embedded in your WEB-INF/classes directory, or inside a JAR file inside WEB-INF/lib (or anywhere on the classpath for Tomcat). There is an additional option that can read any static resource in your WAR file. For example, you can read the "web.xml" file like this: InputStream is = getServletContext().getResourceAsStream("/WEB-INF/web.xml"); which will work even in servlet containers that run your application directly out of a WAR file (instead of an unpacked directory structure like Tomcat does). greetz, christoph Craig McClanahan - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]