1

I am trying to load a properties file. The properites file is in the class path of the application.

 Properties p = new Properties();
 p.load(new FileInputStream("classpath:mail.properties"));
 System.out.println(p.get("hi"));

Now I say classpath, because another file called x.properties is referred in an xml file like this

<property name="x">
    <util:properties location="classpath:x.properties" />
</property>

I placed my mail.properties in the same folder as x.properties, but my Java program is not able to find it ? Any idea what I am missing ?

aioobe
  • 413,195
  • 112
  • 811
  • 826
user2434
  • 6,339
  • 18
  • 63
  • 87

2 Answers2

5

Just because some program processing that XML file likes the syntax classpath:x.properties doesn't mean that it is a universally accepted syntax in Java!

If you provide "classpath:x.properties" to a FileInputStream it will look for a file named classpath:x.properties. (Check the documentation of that particular constructor.)

Try providing the full path to that file. If the file happens to be on your class path, you could use something like

p.load(getClass().getResourceAsStream("mail.properties"));
aioobe
  • 413,195
  • 112
  • 811
  • 826
1

if mail.properties is indeed on your classpath, you will have better luck loading it via a class loader:

Properties p = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream("mail.properties");
p.load(is);
mcfinnigan
  • 11,442
  • 35
  • 28
  • Hah. looks like there was some competition getting this in there first. Popped up just before I clicked post. ;) – Vala Sep 29 '11 at 10:52
  • @Thor84no - lol, your answer clicked up just before *I* clicked post. Time/Space continuum issues? :D – mcfinnigan Sep 29 '11 at 10:59