1

I have my .properties file in

com.someOtherpage
 -somefolder
  --theProperties.java  `<--- This guy needs it`
com.somepackage
WEB-INF
 -config
  --project.properties  `<--- Here is where he sits`

when deployed how can I call the properties file with out calling its absolute path like below

public class theProperties
{
  private static Properties properties = new Properties();

  public theProperties()
  {
  }

  public String get(String attribute) throws Exception
  {
     //what do I need to set up to be able to call this file this way 
     //notice there is no '../../project.properties'
     //                    -----
    InputStream is = theProperties.class.getResourceAsStream("project.properties");
    properties.load(is);

    is.close();

    return properties.getProperty(attribute);
  }
}

The above isn't working, why?

stackoverflow
  • 18,348
  • 50
  • 129
  • 196
  • I thought so too but it doesn't work. There has to be something missing from my build path or something of that nature – stackoverflow Jul 06 '11 at 20:45
  • 1
    Also `theProperties` isn't idiomatic Java. It should be `UpperCamelCase` for class names. –  Jul 06 '11 at 20:49
  • why is downvoted ? stack overflow should not hide persons who downvote or upvote...all should be public ! – Cris Jul 06 '11 at 20:50
  • 1
    @Cris Why do you think so? What do you want to do with that information? That was me BTW. The title and the code don't match, the title initially suggest he's using absolute path and he doesn't. Is until the comment when I realize that piece of code doesn't work for the OP. Do you find the question useful, that's fine. I think it is misleading, but that's just me and downvoting my way to show it. – OscarRyz Jul 06 '11 at 21:07
  • Ok downvote...if you consider ,my comment should have been why downvote without a comment ,a simple explanation is more the ok – Cris Jul 06 '11 at 21:12
  • 2
    Agreed that a comment or edit would have been more appropriate than a downvote: it provides constructive feedback and is more in the spirit of a community wiki. Also agree that down-votes should be public, to encourage more responsible use. – Perception Jul 06 '11 at 21:15
  • I was to first to comment!! :) *You have just done it* I didn't knew that wasn't working ... otherwise I would've put *Pleeaase consider putting if your code is working or not because blah blah blah*. – OscarRyz Jul 06 '11 at 22:12

3 Answers3

4

If you put the properties file in the same package as the Class that reads it you specify its path relative to that class, that is if the properties file is in the exact same package as the class loading it you specify the path as project.properties.

If you put the properties file in the default package and the loading class isn't in the default package, you have to specify an absolute path like, /project.properties. Just a reminder no classes should be in the default class path as a general rule.

Either way, your properties file has to be on the classpath which yours isn't. In other words it has to be somewhere in WEB-INF/classes/.

A better solution, but more complex is to use Guice to inject properties and not write your own reader.

Community
  • 1
  • 1
2

here is a nice explanation of how... http://jaitechwriteups.blogspot.com/2007/01/how-to-read-properties-file-in-web.html

Cris
  • 4,947
  • 6
  • 44
  • 73
  • There are two samples that in my opinion are very clear.One where the prop file is in the root and one where is in a directory present in the root. – Cris Jul 06 '11 at 21:20
  • 1
    And how is that different from what the OP already has? ( just asking, really I cant tell the differences ) – OscarRyz Jul 06 '11 at 22:13
-2

Assuming you want to avoid the absolute filepath, not the absolute path within the classpath, you need to do:

theProperties.class.getResourceAsStream("/WEB-INF/config/project.properties")

The forward slash at the front is important. Without it the path is relative to the loading class's package location.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52