How to read property files?
I would like to put a myapp.properties file in the top level directory of my webapp. But I can't figure out what filepath to give the Properties.load() method in order to load my servlet property object. Can someone help me? Thanks =eas=
Re: How to read property files?
Could you get the resource URL of the class loading the properties file, and then strip off the package and class name? e.g. if Im loading bob.properties from class Bob in package com/fred/, my resource url for the class would be /usr/tomcat/webapps/mycontext/WEB-INF/classes/com/fred/Bob.class. If I strip off the package names and the Bob.class I'd get the path to a props file in the root. You'll have to play around with this if the class is in a jar file. Likewise, when loading the props file using the string name, remember to use File.seperator rather than hardcoding a forward slash. sam - Original Message - From: <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, April 17, 2001 9:27 PM Subject: How to read property files? > I would like to put a myapp.properties file in the top level directory > of my webapp. But I can't figure out what filepath to give the > Properties.load() method in order to load my servlet property object. > Can someone help me? > > Thanks > > =eas= >
RE: How to read property files?
This works a treat for me InputStream is = this.getClass().getResourceAsStream("myapp.properties"); Properties p = new Properties(); try { p.load(is); } catch ( java.io.IOException e ) { // Can't load props file } That way the properties file can be anywhere in the classpath. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 17, 2001 9:27 PM To: [EMAIL PROTECTED] Subject: How to read property files? I would like to put a myapp.properties file in the top level directory of my webapp. But I can't figure out what filepath to give the Properties.load() method in order to load my servlet property object. Can someone help me? Thanks =eas=
Re: How to read property files?
Thanks, this helps me. I was wondering though, can you determine where it found the properties file at? Thanks. -jim Samson, Lyndon [IT] wrote: > This works a treat for me > > InputStream is = this.getClass().getResourceAsStream("myapp.properties"); > Properties p = new Properties(); > try { > p.load(is); > } catch ( java.io.IOException e ) { > // Can't load props file > } > > That way the properties file can be anywhere in the classpath. > > > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, April 17, 2001 9:27 PM > To: [EMAIL PROTECTED] > Subject: How to read property files? > > > I would like to put a myapp.properties file in the top level directory > of my webapp. But I can't figure out what filepath to give the > Properties.load() method in order to load my servlet property object. > Can someone help me? > > Thanks > > =eas= > > >
Re: How to read property files?
I suspect getResourceAsStream can only locate property files in your classpath. You will probably have to search the classpath yourself to do it. Check the sorucecode for getResourceAsStream (assuming its not in native code) and see how its done sam - Original Message - From: "Jim Willeke" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Wednesday, April 18, 2001 2:25 PM Subject: Re: How to read property files? > Thanks, this helps me. > I was wondering though, can you determine where it found the properties > file at? > Thanks. > -jim > > Samson, Lyndon [IT] wrote: > > > This works a treat for me > > > > InputStream is = this.getClass().getResourceAsStream("myapp.properties"); > > Properties p = new Properties(); > > try { > > p.load(is); > > } catch ( java.io.IOException e ) { > > // Can't load props file > > } > > > > That way the properties file can be anywhere in the classpath. > > > > > > -Original Message----- > > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > > Sent: Tuesday, April 17, 2001 9:27 PM > > To: [EMAIL PROTECTED] > > Subject: How to read property files? > > > > > > I would like to put a myapp.properties file in the top level directory > > of my webapp. But I can't figure out what filepath to give the > > Properties.load() method in order to load my servlet property object. > > Can someone help me? > > > > Thanks > > > > =eas= > > > > > > >
RE: How to read property files?
Instead of getResourceAsStream you can use getResource, which returns a URL. You can then break down the URL to determine where it found the resource (and what protocol it used, i.e. file, jar, http, etc). Randy > -Original Message- > From: Jim Willeke [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, April 18, 2001 9:25 AM > To: [EMAIL PROTECTED] > Subject: Re: How to read property files? > > > Thanks, this helps me. > I was wondering though, can you determine where it found the > properties > file at? > Thanks. > -jim > > Samson, Lyndon [IT] wrote: > > > This works a treat for me > > > > InputStream is = > this.getClass().getResourceAsStream("myapp.properties"); > > Properties p = new Properties(); > > try { > > p.load(is); > > } catch ( java.io.IOException e ) { > > // Can't load props file > > } > > > > That way the properties file can be anywhere in the classpath. > > > > > > -Original Message----- > > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > > Sent: Tuesday, April 17, 2001 9:27 PM > > To: [EMAIL PROTECTED] > > Subject: How to read property files? > > > > > > I would like to put a myapp.properties file in the top > level directory > > of my webapp. But I can't figure out what filepath to give the > > Properties.load() method in order to load my servlet > property object. > > Can someone help me? > > > > Thanks > > > > =eas= > > > > > > >
Re: How to read property files?
> > InputStream is = this.getClass().getResourceAsStream("myapp.properties"); > Properties p = new Properties(); > try { > p.load(is); > } catch ( java.io.IOException e ) { > // Can't load props file > } > > That way the properties file can be anywhere in the classpath. > > Have you tried doing this from a class inside a .jar file? I have and it didn't work. > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, April 17, 2001 9:27 PM > To: [EMAIL PROTECTED] > Subject: How to read property files? > > > I would like to put a myapp.properties file in the top level directory > of my webapp. But I can't figure out what filepath to give the > Properties.load() method in order to load my servlet property object. > Can someone help me? > > Thanks > > =eas= >
Re: How to read property files?
You'll want to use getResource when your in a jar file. I've found the safest way is just to know where the property file will be in relation to the class loading it, and get the URL resource for the class itself. This URL will differ for a class in a jar, but its still doable. sam - Original Message - From: "Mark" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Wednesday, April 18, 2001 3:08 PM Subject: Re: How to read property files? > > > > InputStream is = > this.getClass().getResourceAsStream("myapp.properties"); > > Properties p = new Properties(); > > try { > > p.load(is); > > } catch ( java.io.IOException e ) { > > // Can't load props file > > } > > > > That way the properties file can be anywhere in the classpath. > > > > > > Have you tried doing this from a class inside a .jar file? I have > and it didn't work. > >
RE: How to read property files?
Works fine for me? My jar file had a manifest, a class ( default package ) and a properties file. I added the jar to the CLASSPATH and executed the class. Voila! As the InputStream has no concept of file paths there is no easy way to determine where in the CLASSPATH it was loaded from. CLASSPATH search order is actually undefined allthough it tends to left to right. The only way I can think of is to split System.getProperties().getProperty("java.class.path") and append the properties file name to every path and then checking if that file is readable. Yuk. -Original Message- From: Mark [mailto:[EMAIL PROTECTED]] Sent: Wednesday, April 18, 2001 3:08 PM To: [EMAIL PROTECTED] Subject: Re: How to read property files? > > InputStream is = this.getClass().getResourceAsStream("myapp.properties"); > Properties p = new Properties(); > try { > p.load(is); > } catch ( java.io.IOException e ) { > // Can't load props file > } > > That way the properties file can be anywhere in the classpath. > > Have you tried doing this from a class inside a .jar file? I have and it didn't work. > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, April 17, 2001 9:27 PM > To: [EMAIL PROTECTED] > Subject: How to read property files? > > > I would like to put a myapp.properties file in the top level directory > of my webapp. But I can't figure out what filepath to give the > Properties.load() method in order to load my servlet property object. > Can someone help me? > > Thanks > > =eas= >
Re: How to read property files?
>> Have you tried doing this from a class inside a .jar file? I have >> and it didn't work. > Works fine for me? My jar file had a manifest, a class ( default package ) > and a properties file. I added the jar to the CLASSPATH and executed the > class. Voila! The properties file is not in the jar. Just the class that tries to access it. I didn't try it with the properties file inside the jar, so it may work. But I don't want to do that. I want to make it readily editable. Mark - Original Message - From: "Samson, Lyndon [IT]" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Wednesday, April 18, 2001 10:42 AM Subject: RE: How to read property files? > Works fine for me? My jar file had a manifest, a class ( default package ) > and a properties file. I added the jar to the CLASSPATH and executed the > class. Voila! > > As the InputStream has no concept of file paths there is no easy way to > determine where > in the CLASSPATH it was loaded from. CLASSPATH search order is actually > undefined allthough it tends to left to right. The only way I can think of > is to split System.getProperties().getProperty("java.class.path") and append > the > properties file name to every path and then checking if that file is > readable. Yuk. > > > -Original Message- > From: Mark [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, April 18, 2001 3:08 PM > To: [EMAIL PROTECTED] > Subject: Re: How to read property files? > > > > > > InputStream is = > this.getClass().getResourceAsStream("myapp.properties"); > > Properties p = new Properties(); > > try { > > p.load(is); > > } catch ( java.io.IOException e ) { > > // Can't load props file > > } > > > > That way the properties file can be anywhere in the classpath. > > > > > > > > > -Original Message- > > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > > Sent: Tuesday, April 17, 2001 9:27 PM > > To: [EMAIL PROTECTED] > > Subject: How to read property files? > > > > > > I would like to put a myapp.properties file in the top level directory > > of my webapp. But I can't figure out what filepath to give the > > Properties.load() method in order to load my servlet property object. > > Can someone help me? > > > > Thanks > > > > =eas= > > >
RE: How to read property files?
Make sure the directory with the properties file in it is part of your CLASSPATH. Ie add . if the properties is in the current directory. -Original Message- From: Mark [mailto:[EMAIL PROTECTED]] Sent: Wednesday, April 18, 2001 4:18 PM To: [EMAIL PROTECTED] Subject: Re: How to read property files? >> Have you tried doing this from a class inside a .jar file? I have >> and it didn't work. > Works fine for me? My jar file had a manifest, a class ( default package ) > and a properties file. I added the jar to the CLASSPATH and executed the > class. Voila! The properties file is not in the jar. Just the class that tries to access it. I didn't try it with the properties file inside the jar, so it may work. But I don't want to do that. I want to make it readily editable. Mark - Original Message - From: "Samson, Lyndon [IT]" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Wednesday, April 18, 2001 10:42 AM Subject: RE: How to read property files? > Works fine for me? My jar file had a manifest, a class ( default package ) > and a properties file. I added the jar to the CLASSPATH and executed the > class. Voila! > > As the InputStream has no concept of file paths there is no easy way to > determine where > in the CLASSPATH it was loaded from. CLASSPATH search order is actually > undefined allthough it tends to left to right. The only way I can think of > is to split System.getProperties().getProperty("java.class.path") and append > the > properties file name to every path and then checking if that file is > readable. Yuk. > > > -Original Message- > From: Mark [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, April 18, 2001 3:08 PM > To: [EMAIL PROTECTED] > Subject: Re: How to read property files? > > > > > > InputStream is = > this.getClass().getResourceAsStream("myapp.properties"); > > Properties p = new Properties(); > > try { > > p.load(is); > > } catch ( java.io.IOException e ) { > > // Can't load props file > > } > > > > That way the properties file can be anywhere in the classpath. > > > > > > > > > -Original Message- > > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > > Sent: Tuesday, April 17, 2001 9:27 PM > > To: [EMAIL PROTECTED] > > Subject: How to read property files? > > > > > > I would like to put a myapp.properties file in the top level directory > > of my webapp. But I can't figure out what filepath to give the > > Properties.load() method in order to load my servlet property object. > > Can someone help me? > > > > Thanks > > > > =eas= > > >
RE: How to read property files?
don't mess around with the system classpath. makes your webapp non-portable. take advantage of the fact that you are using a war structure. put the property file under WEB-INF/classes/ then do this java.util.Properties prop = new java.util.Properties(); prop.load( Thread.currentThread().getContextClassLoader().getResourceAsStrea m("mypropertyfile.properties") ); that should do the trick, you may also want to try "./mypropertyfile.properties" and "/mypropertyfile.properties" if the file doesn't get picked up Filip ~ Namaste - I bow to the divine in you ~ Filip Hanik Software Architect [EMAIL PROTECTED] www.filip.net > -Original Message- > From: Samson, Lyndon [IT] [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, April 18, 2001 8:30 AM > To: [EMAIL PROTECTED] > Subject: RE: How to read property files? > > > Make sure the directory with the properties file in it is part of your > CLASSPATH. Ie add . if the properties is in the current directory. > > > -Original Message- > From: Mark [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, April 18, 2001 4:18 PM > To: [EMAIL PROTECTED] > Subject: Re: How to read property files? > > > >> Have you tried doing this from a class inside a .jar file? I have > >> and it didn't work. > > > > Works fine for me? My jar file had a manifest, a class > default package ) > > and a properties file. I added the jar to the CLASSPATH and executed the > > class. Voila! > > The properties file is not in the jar. Just the class that tries > to access it. I didn't try it with the properties file inside the > jar, so it may work. But I don't want to do that. I want > to make it readily editable. > > Mark > > ----- Original Message - > From: "Samson, Lyndon [IT]" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Wednesday, April 18, 2001 10:42 AM > Subject: RE: How to read property files? > > > > Works fine for me? My jar file had a manifest, a class > default package ) > > and a properties file. I added the jar to the CLASSPATH and executed the > > class. Voila! > > > > As the InputStream has no concept of file paths there is no easy way to > > determine where > > in the CLASSPATH it was loaded from. CLASSPATH search order is actually > > undefined allthough it tends to left to right. The only way I > can think of > > is to split System.getProperties().getProperty("java.class.path") and > append > > the > > properties file name to every path and then checking if that file is > > readable. Yuk. > > > > > > -Original Message- > > From: Mark [mailto:[EMAIL PROTECTED]] > > Sent: Wednesday, April 18, 2001 3:08 PM > > To: [EMAIL PROTECTED] > > Subject: Re: How to read property files? > > > > > > > > > > InputStream is = > > this.getClass().getResourceAsStream("myapp.properties"); > > > Properties p = new Properties(); > > > try { > > > p.load(is); > > > } catch ( java.io.IOException e ) { > > > // Can't load props file > > > } > > > > > > That way the properties file can be anywhere in the classpath. > > > > > > > > > > > > > > > -Original Message- > > > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > > > Sent: Tuesday, April 17, 2001 9:27 PM > > > To: [EMAIL PROTECTED] > > > Subject: How to read property files? > > > > > > > > > I would like to put a myapp.properties file in the top level directory > > > of my webapp. But I can't figure out what filepath to give the > > > Properties.load() method in order to load my servlet property object. > > > Can someone help me? > > > > > > Thanks > > > > > > =eas= > > > > > >
Re: How to read property files?
Thanks all that answered. I tried several of the suggestions, and here is what I finally came up with that works for me. I think it is portable enough to use in most any tomcat situation. File pf = new File(request.getRealPath("/myapp.properties")); BufferedInputStream is = new BufferedInputStream(new FileInputStream(pf)); Properties p = new Properties(); try { p.load(is); } catch ( java.io.IOException e ) { out.print("Can't load the properties file"); return; } =eas= On 18 Apr, Filip Hanik wrote: > don't mess around with the system classpath. makes your webapp > non-portable. take advantage of the fact that you are using a war > structure. > > put the property file under WEB-INF/classes/ > > then do this > > java.util.Properties prop = new java.util.Properties(); > prop.load( > Thread.currentThread().getContextClassLoader().getResourceAsStrea > m("mypropertyfile.properties") ); > > that should do the trick, > > you may also want to try > "./mypropertyfile.properties" and "/mypropertyfile.properties" if the > file doesn't get picked up > > Filip > > ~ > Namaste - I bow to the divine in you > ~ > Filip Hanik > Software Architect > [EMAIL PROTECTED] > www.filip.net > >> -Original Message- >> From: Samson, Lyndon [IT] [mailto:[EMAIL PROTECTED]] >> Sent: Wednesday, April 18, 2001 8:30 AM >> To: [EMAIL PROTECTED] >> Subject: RE: How to read property files? >> >> >> Make sure the directory with the properties file in it is part of >> your CLASSPATH. Ie add . if the properties is in the current >> directory. >> >> >> -Original Message- >> From: Mark [mailto:[EMAIL PROTECTED]] >> Sent: Wednesday, April 18, 2001 4:18 PM >> To: [EMAIL PROTECTED] >> Subject: Re: How to read property files? >> >> >> >> Have you tried doing this from a class inside a .jar file? I >> >> have and it didn't work. >> >> >> > Works fine for me? My jar file had a manifest, a class > >> default package ) >> > and a properties file. I added the jar to the CLASSPATH and >> > executed the class. Voila! >> >> The properties file is not in the jar. Just the class that tries >> to access it. I didn't try it with the properties file inside the >> jar, so it may work. But I don't want to do that. I want >> to make it readily editable. >> >> Mark >> >> - Original Message - >> From: "Samson, Lyndon [IT]" <[EMAIL PROTECTED]> >> To: <[EMAIL PROTECTED]> >> Sent: Wednesday, April 18, 2001 10:42 AM >> Subject: RE: How to read property files? >> >> >> > Works fine for me? My jar file had a manifest, a class > >> default package ) >> > and a properties file. I added the jar to the CLASSPATH and >> > executed the class. Voila! >> > >> > As the InputStream has no concept of file paths there is no easy >> > way to determine where in the CLASSPATH it was loaded from. >> > CLASSPATH search order is actually undefined allthough it tends to >> > left to right. The only way I >> can think of >> > is to split System.getProperties().getProperty("java.class.path") >> > and >> append >> > the >> > properties file name to every path and then checking if that file >> > is readable. Yuk. >> > >> > >> > -Original Message- >> > From: Mark [mailto:[EMAIL PROTECTED]] >> > Sent: Wednesday, April 18, 2001 3:08 PM >> > To: [EMAIL PROTECTED] >> > Subject: Re: How to read property files? >> > >> > >> > > >> > > InputStream is = >> > this.getClass().getResourceAsStream("myapp.properties"); >> > > Properties p = new Properties(); >> > > try { >> > > p.load(is); >> > > } catch ( java.io.IOException e ) { >> > > // Can't load props file >> > > } >> > > >> > > That way the properties file can be anywhere in the classpath. >> > > >> > > >> > >> > >> > >> > > -Original Message- >> > > From: [EMAIL PROTECTED] >> > > [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 17, 2001 >> > > 9:27 PM To: [EMAIL PROTECTED] Subject: How to read >> > > property files? >> > > >> > > >> > > I would like to put a myapp.properties file in the top level >> > > directory of my webapp. But I can't figure out what filepath to >> > > give the Properties.load() method in order to load my servlet >> > > property object. Can someone help me? >> > > >> > > Thanks >> > > >> > > =eas= >> > > >> > >> > ~~~
RE: How to read property files?
Sorry to tell you that it does work. I've tried used this on various platforms with various software deployed on various platforms, like Jserv, Tomcat, WebLogic, Jrun on Win2000, Linux, Solaris, HP-UX. So it's fair to say that this contruct does work ;) Wouter -Original Message- From: Mark [mailto:[EMAIL PROTECTED]] Sent: 18 April 2001 16:08 To: [EMAIL PROTECTED] Subject: Re: How to read property files? > > InputStream is = this.getClass().getResourceAsStream("myapp.properties"); > Properties p = new Properties(); > try { > p.load(is); > } catch ( java.io.IOException e ) { > // Can't load props file > } > > That way the properties file can be anywhere in the classpath. > > Have you tried doing this from a class inside a .jar file? I have and it didn't work. > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, April 17, 2001 9:27 PM > To: [EMAIL PROTECTED] > Subject: How to read property files? > > > I would like to put a myapp.properties file in the top level directory > of my webapp. But I can't figure out what filepath to give the > Properties.load() method in order to load my servlet property object. > Can someone help me? > > Thanks > > =eas= >