【发布时间】:2014-03-26 19:48:31
【问题描述】:
我过去 2 个小时只是为了在我的 IntelliJ IDEA 类路径中添加一个属性文件。我是一个 Eclipse 用户,发现我无法在 IDEA 中做到这一点很尴尬。我的 IntelliJ IDEA 项目中有一个 .properties 文件,它已添加到类路径中,但在运行时我仍然收到文件不存在的异常。
我已经关注了这个问题Add a properties file to IntelliJ's classpath的所有答案
我做了以下事情:-
1) 转到项目结构。 选择您的模块。 在右侧的树中找到文件夹并选择它。 单击该树上方的 Sources 按钮(带有蓝色文件夹),使该文件夹成为源文件夹。
2)我检查了 settings->compiler->resource patterns 有 ?*.properties 的条目
3) 我在 pom.xml 中添加了以下标签
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
有人可以帮我吗?
进一步更新。这是一个 Maven Web 应用程序项目。我正在尝试访问我的 servlet 中的 .properties 文件。我的 servlet 位于 src/main/java/some_package 中,我的 .properties 文件位于 src/main/resources/some_package 中。
try {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("package\\myProperty.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println("reading the property file " );
System.out.println("prop1 =" + prop.getProperty("prop1"));
System.out.println("prop2 = " + prop.getProperty("prop2"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
有人可以在这里帮忙吗?这不应该这么难:(
-
您能否提供更多有关该错误的信息以及如何在运行时使用您的
properties文件。我的意思是一些代码示例、配置和异常堆栈跟踪。 -
@EugeneEvdokimov - 我添加了更多细节。请检查。
-
如果你使用Maven构建项目,结果不应该依赖于你工作的IDE,无论是Eclipse还是IDEA。关于 SO 的问题有很多很好的答案:stackoverflow.com/a/2308388/546117、stackoverflow.com/a/12523396/546117、stackoverflow.com/a/18508032/546117。希望他们会有所帮助。
-
感谢@EugeneEvdokimov,但问题是我的代码没有直接加载这个属性文件。我正在调用一些第三方 jar,而后者又试图加载此属性文件。所以我不能真正尝试改变文件的加载方式。我唯一需要确定的是它应该在类路径中。目前我已经将它添加到tomcat的lib文件夹中,但我需要一个永久的解决方案来解决这个问题。
标签: intellij-idea properties-file