Discuss / Java / 【疑问】-读取配置文件

【疑问】-读取配置文件

Topic source

读取配置文件

Properties props = new Properties();
props.load(getClass().getResourceAsStream("/common/setting.properties"));

这里的  degClass()方法应该是对某个实例的方法调用,

这里都没有实例对象呀

直接放在编译器里会报错呢,

请问呢是什么原因呢

报错信息

Cannot make a static reference to the non-static method getClass() from the type Object

关键在于获取到该类的class。

因为main是静态方法没法用实例来获取所在类的class,在main方法里可以通过调用其所在的public类的类名.class(如Test.class)获得该类的class,即可调用该函数:

props.load(Test.class.getResourceAsStream("/common/setting.properties"));

Joker.fu_95

#3 Created at ... [Delete] [Delete and Lock User]

原文是没有放在main中的代码,也就是当前对象


  • 1

Reply