【发布时间】:2013-08-08 11:59:51
【问题描述】:
在main() 类中我放了一个代码:
App.getClass().getClassLoader().getResourceAsStream("Repo-Offer.properties")
结果是null
属性文件位于:
Project/src/main/resources/properties/Repo-Offer.properties
我试图加载这样的属性:
private Properties getPropertiesFromClasspath(String propFileName) throws IOException {
// loading xmlProfileGen.properties from the classpath
Properties props = new Properties();
InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream(propFileName);
if (inputStream == null) {
throw new FileNotFoundException("property file '" + propFileName
+ "' not found in the classpath");
}
props.load(inputStream);
return props;
}
但因为它说的是 null
Exception in thread "main" java.io.FileNotFoundException: Repo-Offer.properties
如何使用来自以下位置的属性文件: 项目/src/main/resources/properties/Repo-Offer.properties 而源在
Project/src/main/java/com/...
? 编辑 它是 Maven 项目。
【问题讨论】:
-
尝试加载
getResourceAsStream("properties/Repo-Offer.properties")。有用吗? -
这真的是一个Maven项目吗?您是如何创建和运行最终工件的?
-
@Dave Newton 是的,它是 maven 项目。从 Eclipse GUI 界面运行它。
-
@LaurentG 在下面看到我的答案
标签: java properties classpath filenotfoundexception