aspose file tools*
The moose likes Servlets and the fly likes Problem loading Properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem loading Properties file" Watch "Problem loading Properties file" New topic
Author

Problem loading Properties file

Bijju Kranthi
Greenhorn

Joined: Jul 11, 2007
Posts: 6
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

Joined: Apr 01, 2007
Posts: 20
Replace the code loading the file with this snippet.
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
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 ]

Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
Bijju Kranthi
Greenhorn

Joined: Jul 11, 2007
Posts: 6
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

Joined: Aug 12, 2008
Posts: 12
Class loader only loads classes not property files


http://www.thejavacode.com/
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jul 11, 2007
Posts: 6
Thanks a lot!!!
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

No charge, and welcome to the Ranch
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2713
    
    5

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


SCJP, SCWCD.
|Asking Good Questions|
Bijju Kranthi
Greenhorn

Joined: Jul 11, 2007
Posts: 6
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Oct 26, 2006
Posts: 218
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem loading Properties file
 
Similar Threads
Load Property file in servlet or jsp
getResourceAsStream using getClass() mehtod
How Load Props File Not In Classpath in WebApp Without Hardcoding Full Path?
Load a configuration file
Problem with using getResourceAsStream