views:

65

answers:

3

I am debugging in Eclipse and Tomcat - and I want to load in a property from a file called foo.properties. The file is stored in src directory, like so:

alt text

The following variable is always null:

java.net.URL url = loader.getResource(propFile);

which is becaue (I am guessing) the ClassLoader could locate the foo.properties file.

The properties file only has the text in it:

foo=bar

This is the full code I am using:

private static java.util.Properties prop = new java.util.Properties();
private static void loadProperties() {
// get class loader
ClassLoader loader = Config.class.getClassLoader();
if (loader == null)
 loader = ClassLoader.getSystemClassLoader();

 String propFile = "foo.properties";
 java.net.URL url = loader.getResource(propFile);
 try {
  prop.load(url.openStream()); //ERROR HERE
 } catch (Exception e) {
 System.err.println("Could not load configuration file: " + propFile);
 }
}

This is where I got the code snippet from source code

How do I get this file loaded properly?

+2  A: 

Tried using ServletContext#getResourceAsStream(java.lang.String)?

Read http://download.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getResourceAsStream%28java.lang.String%29 for more details

madhurtanwani
As @duffymo has suggested - are you sure after packaging the property file is present in the WEB-INF/classes dir?
madhurtanwani
A: 

You might be missing out the package path from your resource name? But that is only a guess.

loader.getResource("com/mypackage/my-properties.properties");
Adrian Smith
No, if it's in WEB-INF/classes the web app class loader will find it.
duffymo
Ah, OK, the question has been updated since I wrote my answer.
Adrian Smith
A: 

Another way to do it is to learn Spring and use its PropertyPlaceHolderConfigurer

It's overkill for such a simple problem, but you and your app will benefit in lots of other ways.

UPDATE:

The file is stored in src directory

This is the root of your problem.

You need to tell Eclipse how to package your app in such a way that the foo.properties file ends up in the WEB-INF/classes directory of your WAR. Once you figure out how to do that, your app can access the file easily using ServletContext.getResourceAsStream() method.

duffymo
I'm sure, but that doesn't make my answer wrong. Other people besides you read this site. It's a plus if they are made aware of a helpful solution by my answer.
duffymo
Like I said, this site isn't just about you. And if you think you need to learn all of Spring (you don't) and Hibernate (they aren't joined at the hip - where do you see a mention of Hibernate?), then you don't have a very good understanding of either one.
duffymo
I see your point, but we disagree about the magnitude of that "fair bit of understanding". You have to have a fair bit of understanding to write Java at all. I'm giving you the benefit of the doubt. Like I said, this site isn't just about your and the narrow problem at hand. You've already voted me down. You've got a solution. Why persist in the argument? Per my update, maybe your real problem is that you don't understand Eclipse or how to properly package a web app. How many hours will you waste on that?
duffymo
I understand Vidar, no worries. I hope this helped.
duffymo
Cleared up comments...
Vidar