• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem loading Properties file

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a properties file in the following path :
/WebContent/WEB-INF/systemoperations.properties

I am trying to load the properties file in my servlet init() method.

I tried the following code:
Properties properties =null;
InputStream inputStream = ClassLoader.getSystemResourceAsStream("WEB-INF/serviceoperations.properties");
properties = new Properties();
properties.load(inputStream);


but this piece of code is throwing a null pointer exception.

Please help me resolve this problem.

I am using Eclipse Europa.

Thanks!
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Replace the code loading the file with this snippet.
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pheonixashes,

ClassLoaders are typically used to load the binary name of a class. For more information on its usage please refer

ClassLoader
[ August 12, 2008: Message edited by: Schandha Ravi ]
 
Bijju Kranthi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks it worked.

May I know why the previous code didn't work?

I also tried the following:

InputStream inputStream = ctx.getResourceAsStream(ctx.getRealPath("serviceoperations.properties"));

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

InputStream inputStream =classLoader.getResourceAsStream("serviceoperations.properties");
URL url=classLoader.getResource("serviceoperations.properties");

I didnt understand why the above failed to load the file

Thanks very much!
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class loader only loads classes not property files
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"pheonixashes11",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted.
[ August 12, 2008: Message edited by: David O'Meara ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Muhammad Safwat:
Class loader only loads classes not property files



Not correct. The ClassLoader can be used to find any resource on the classpath and return an InputStream or URL, as is displayed in the examples above.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pheonixashes11:
May I know why the previous code didn't work?



The ClassLoader can only load from the ClassPath and the WEB-INF directory is not on the classpath. If you placed the properties in the WEB-INF/classes directory, you could have found the resource as /serviceoperations.properties

While finding resources in web applications via the context is useful in web applications, I prefer to default to the classpath mechanism as it will work in regular applications too.
 
Bijju Kranthi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!!!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No charge, and welcome to the Ranch
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I may not be saying this but if you read the link sheriff Dave provided above then it says


You can even use an initial or initials for the first name if you like (but not the last).


Sorry for pointing this out
 
Bijju Kranthi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have one more doubt.

How do I load the properties file when I am in a java bean?
I donot have a servlet config and context there right.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bijju Kranthi:
How do I load the properties file when I am in a java bean?
I donot have a servlet config and context there right.



I assume, your bean is a simple POJO, with getters and setters. Having said that, why do you need to load the property file in your bean? Some other place might be better in this case. What say you.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to load a properties file from a standalone java application you can use Classpath.getResourceAsStream() and the load the properties using Properties.load() Make sure your properties file is in classpath.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic