Java 将 .properties 文件放在 Eclipse 项目中的什么位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/762475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
where to put .properties files in an Eclipse project?
提问by Jason S
I have a very simple properties file test I am trying to get working: (the following is TestProperties.java)
我有一个非常简单的属性文件测试我正在尝试工作:(以下是 TestProperties.java)
package com.example.test;
import java.util.ResourceBundle;
public class TestProperties {
public static void main(String[] args) {
ResourceBundle myResources =
ResourceBundle.getBundle("TestProperties");
for (String s : myResources.keySet())
{
System.out.println(s);
}
}
}
and TestProperties.properties in the same directory:
和 TestProperties.properties 在同一目录中:
something=this is something
something.else=this is something else
which I have also saved as TestProperties_en_US.properties
我也保存为 TestProperties_en_US.properties
When I run TestProperties.java from Eclipse, it can't find the properties file:
当我从 Eclipse 运行 TestProperties.java 时,它找不到属性文件:
java.util.MissingResourceException:
Can't find bundle for base name TestProperties, locale en_US
Am I doing something wrong?
难道我做错了什么?
采纳答案by Jon Skeet
Put it at the root level of one of your source paths, or fully qualify the resource name in the call to getBundle
, e.g.
将其放在源路径之一的根级别,或在调用中完全限定资源名称getBundle
,例如
ResourceBundle myResources =
ResourceBundle.getBundle("com.example.test.TestProperties");
See the docs for ResourceBundle.getBundle(String, Locale, ClassLoader)
for more information.
有关ResourceBundle.getBundle(String, Locale, ClassLoader)
更多信息,请参阅文档。
回答by Jason S
Aha, thanks a bunch. This also works.
啊哈,非常感谢。这也有效。
package com.example.test;
import java.util.ResourceBundle;
public class TestProperties {
public static void main(String[] args) {
ResourceBundle myResources =
ResourceBundle.getBundle(TestProperties.class.getCanonicalName());
for (String s : myResources.keySet())
{
System.out.println(s);
}
}
}
回答by Shannon Gerry
I have just been trying to solve this problem as well, I have found that you must refresh Eclipse's list of files before you try to run your project. Then you can have your files in the base directory and use them as normal.
我也一直在尝试解决这个问题,我发现在尝试运行项目之前必须刷新 Eclipse 的文件列表。然后,您可以将文件放在基本目录中并照常使用它们。
回答by sensoft1988
put the TestProperties_en_US.properties(propery) file in the src folder and then run the program it will run
将 TestProperties_en_US.properties(propery) 文件放在 src 文件夹中,然后运行它将运行的程序
回答by JozefP
Do NOT put your propierties files into your src folder! Obviously that works, but basically this is NOT how you should approach your problems. Create a new folder in your project, for instance a 'Resources' folder, add it to the classpath in project properties and put all files other than .java there.
不要将您的属性文件放入您的 src 文件夹中!显然这是有效的,但基本上这不是你应该如何解决你的问题。在您的项目中创建一个新文件夹,例如“Resources”文件夹,将其添加到项目属性的类路径中,并将除 .java 之外的所有文件放在那里。