user2434
user2434

Reputation: 6399

Not able to load properties file in Java

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 ?

Upvotes: 1

Views: 6454

Answers (2)

aioobe
aioobe

Reputation: 420921

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"));

Upvotes: 5

mcfinnigan
mcfinnigan

Reputation: 11638

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);

Upvotes: 1

Related Questions