开发者

Not able to load properties file in Java

开发者 https://www.devze.com 2023-04-09 01:29 出处:网络
I am trying to load a properties file. The properites file is in the class path of the application. Properties p = new Properties();

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"开发者_Python百科));
 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 ?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号