aspose file tools
The moose likes Java in General and the fly likes Read from properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Read from properties file" Watch "Read from properties file" New topic
Author

Read from properties file

Sherryl Philip
Greenhorn

Joined: Jul 02, 2004
Posts: 20
Hi,
I need to know how to read some values of certain keys from the properties file. this is possible when i tried to do in the Tomcat server but this was not possible when i tried out with the Jboss server. Why is this so??

please do help me out.


public class SampleResource {
public SampleResource() {}
public String callResourcebundle() {
ResourceBundle bundle = ResourceBundle.getBundle("MyResourceBundle");
String emailAdd = bundle.getString("emailAdd");
return emailAdd;
}
}

And then I tried to get it from another class..

SampleResource sample = new SampleResource();
String adminEmail = sample.callResourcebundle();


The same code is being put in the both the servers, but it doesnt work on Jboss server where Tomcat is bundled with it. Why should this kind of problem arise, even when the code is the same?

Please do help me out. Thanks in advance...
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Where is MyResourceBundle? It must be in the classpath to be found as a resource.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sherryl Philip
Greenhorn

Joined: Jul 02, 2004
Posts: 20
Paul,
I've put MyResourcebundle.properties in the src folder for it to locate the properties file. why does it take time to even find the properties file???/

thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

In the src folder? I'm afraid you've lost me - JBoss doesn't have a src folder (unless you are talking about the actual JBoss source, in which case you've put it in the wrong place).

Why does it take time to find your properties file? Here's how ResourceBundles work. It implements a cache of ResourceBundles (i.e. already loaded properties files). If you call
it first looks for the resource in the cache. If its not there it tries to load the resource via the ClassLoader. In this case its actually looking for a class, not a properties file which represents your resource. If thts unsuccessful, it starts to look for a properties file. To do that it calls he ClassLoader method "getSystemResourceAsStream", which walks the hierarchy of ClassLoaders, starting with the parent ClassLoader looking for the file.

So that's why its not instantaneous. What that all means is that for your properties file to be found it must be available to a ClassLoader. And all a ClassLoader can see is whats in the classpath. So (and this is probably all you really wanted to hear) if you want the ClassLoader to find this file try putting it in the root of the application you've deployed onJBoss.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Read from properties file
 
Similar Threads
Unable to read properties file from Servlet
help:bean:write value from bean
Deploying a webservice with isolated namespace
Creating a text file that is a HashTable?
Doubt on Servlet having database connection?