Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem loading Properties file

 
Bijju Kranthi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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!
 
Abhijeet Nalawade
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replace the code loading the file with this snippet.
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Muhammad Safwat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class loader only loads classes not property files
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks a lot!!!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No charge, and welcome to the Ranch
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • 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.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic