Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Loading from classpath dont work


Back to:
www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html
.

ClassLoader loader = ClassLoader.getSystemClassLoader();

System.out.println(loader.getClass().getName());

InputStream in = null;
if (loader != null) {
URL url = ClassLoader.getSystemResource("com/comp/resource/workflow.properties");

String path = System.getProperty("catalina.home") + File.separator+ "workflow.properties";
System.out.println("Path"+path);
if (url == null) {
in = loader.getResourceAsStream("com/comp/resource/workflow.properties");

if (in != null) {
props = new Properties();
props.load(in);
System.out.println("property val :" + props.getProperty("PushAppImpl"));
}

}
if (url != null) {

in = url.openStream();
System.out.println("property val :" + props.getProperty("PushAppImpl"));
} else {
System.out.println("STILL NULL");
}

NO matter what it gives Back Null
the jat filwe is in the LIB directory why is it not able to locate the propertiers file?

Your rating: None Average: 3.3 (3 votes)

Try using Properties prop =

Try using

Properties prop = new Properties();
String pwd = System.getProperty("user.dir");
prop.load(new FileInputStream(new File(pwd+"\\com\\comp\\resource\\workflow.properties")));

Play around with the path, you'll figure it out...

Class Loader issue

Rather than using ClassLoader.getClassloader(), user this.getClass().getClassLoader() to load a resource.

It seems you are trying to

It seems you are trying to load properties file, the best way to load a properties file is to use 'ResourceBundle' class from java.util.* package.

e.g
ResourceBundle rb = ResourceBundle.getBundle("com.comp.resource.workflow");

/*get the value for the key*/
rb.getObject(keyProperty)

Not necessarily

ResourceBundles have two advantages: They are implicitly locale-aware and they can (using ListResourceBundle) be classes providing non-text resources. However, if you read the file often then a ResourceBundle approach will be costly since it will try multiple varieties of locale-infixed names like "workflow_en_US.properties" etc. before using the fallback non-decorated name.

classpath

It seems you are trying to load properties file, the best way to load a properties file is to use 'ResourceBundle' class from java.util

Loading from classpath dont work

is working quite well thank you I have not experienced any problems..

Properties file loading issue

I have put myconfig.properties file under classes folder as ./classes/myconfig.properties while the code to load this file is under ./classes/com/example/TestLoader. I try to load this file as props.load(TestLoader.class.getClassLoader().getResourceAsStream(PROPS_FILE)); but somehow its not loading the file.

Resources

When using Resources try to put the resource being loaded in the resource directory of the project.

Thanks for sharing this

Thanks for sharing this information. I found it very informative as I have been researching a lot lately on practical matters such as you talk about...

not reloading new values in web app

Hi, I have used this with dynamic property loading sample in my application but not work .its not showing new values being modified in Jboss/tomcat.Please help me.

Hi, I have used, Properties

Hi,
I have used,
Properties prop = new Properties();
prop.load(new FileInputStream(new File("sample.properties")));
and placed the "sample.properties" in Tomcat's "bin" folder. It worked.

I find that going with the (via FileInputStream, FileReader, and

I find that going with the (via FileInputStream, FileReader, and RandomAccessFile) route is the simplest and usually one of the most effective.

Best Strategy Indeed!

thanks for sharing the best strategy for loading property and configuration files in Java.

Good

Thanks ,excellent presentaion of very important details

its seems helping lets see

its seems helping lets see

A new set of extensions to the properties syntax

Properties have not really been updated since Java 1.0. A new open source project is filling the gap - with variable substitution, inclusion, lists, nesting, etc. Have a look at http://code.google.com/p/eproperties/

Interesting

The conversation is of great interest. It seems to me it is possible to find out here more than from all the magazines and books (I found a very nice selection at rapidshare SE http://rapidpedia.com ).
Thanks, it was of great help.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.