java.util.MissingResourceException: 找不到基本名称“property_file name”的包,区域设置 en_US
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20142002/
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
java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US
提问by roger_that
I am trying to create a utility class ReadPropertyUtil.java
for reading data from property file. While my class is located under a util directory , my skyscrapper.properties
file is placed in some other directory.
我正在尝试创建一个实用程序类ReadPropertyUtil.java
来从属性文件中读取数据。虽然我的班级位于 util 目录下,但我的skyscrapper.properties
文件位于其他目录中。
But , when i try to access the properties using [ResourceBundle][1]
, i get exceptions, that bundle can't be loaded.
但是,当我尝试使用 访问属性时[ResourceBundle][1]
,出现异常,无法加载该包。
Below is the code on how I am reading the properties and also an image which shows my directory structure.
下面是关于我如何读取属性的代码以及显示我的目录结构的图像。
ReadPropertiesUtil.java
ReadPropertiesUtil.java
/**
* Properties file name.
*/
private static final String FILENAME = "skyscrapper";
/**
* Resource bundle.
*/
private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);
/**
* Method to read the property value.
*
* @param key
* @return
*/
public static String getProperty(final String key) {
String str = null;
if (resourceBundle != null) {
str = resourceBundle.getString(key);
LOGGER.debug("Value found: " + str + " for key: " + key);
} else {
LOGGER.debug("Properties file was not loaded correctly!!");
}
return str;
}
Directory Structure
目录结构
This line is giving the error private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);
这一行给出了错误 private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);
I am unable to understand why isn't this working and what is the solution. The src
folder is already added in build path completely.
我无法理解为什么这不起作用以及解决方案是什么。该src
文件夹已完全添加到构建路径中。
采纳答案by Bludzee
Try with the fully qualified namefor the resource:
尝试使用资源的完全限定名称:
private static final String FILENAME = "resources/skyscrapper";
回答by Ross Drew
ResourceBundledoesn't load files? You need to get the files into a resource first. How about just loading into a FileInputStreamthen a PropertyResourceBundle
ResourceBundle不加载文件?您需要先将文件放入资源中。如何只加载到FileInputStream然后加载一个PropertyResourceBundle
FileInputStream fis = new FileInputStream("skyscrapper.properties");
resourceBundle = new PropertyResourceBundle(fis);
Or if you need the locale specific code, something like this should work
或者,如果您需要特定于语言环境的代码,这样的事情应该可以工作
File file = new File("skyscrapper.properties");
URL[] urls = {file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle rb = ResourceBundle.getBundle("skyscrapper", Locale.getDefault(), loader);
回答by Sudeep Krishnan M
Use the Resource like
像这样使用资源
ResourceBundle rb = ResourceBundle.getBundle("com//sudeep//internationalization//MyApp",locale);
or
ResourceBundle rb = ResourceBundle.getBundle("com.sudeep.internationalization.MyApp",locale);
Just give the qualified path .. Its working for me!!!
只要给出合格的路径..它对我有用!!!
回答by Alireza Alallah
You should set property file name without .properties
extension,
it works correctly for me:)
您应该设置没有.properties
扩展名的属性文件名,它对我来说可以正常工作:)
回答by peihan
I'd like to share my experience of using Ant in building projects, *.properties files should be copied explicitly. This is because Ant will not compile *.properties files into the build working directory by default (javac just ignore *.properties). For example:
我想分享我在构建项目中使用 Ant 的经验,*.properties 文件应该被明确复制。这是因为默认情况下 Ant 不会将 *.properties 文件编译到构建工作目录中(javac 只是忽略 *.properties)。例如:
<target name="compile" depends="init">
<javac destdir="${dst}" srcdir="${src}" debug="on" encoding="utf-8" includeantruntime="false">
<include name="com/example/**" />
<classpath refid="libs" />
</javac>
<copy todir="${dst}">
<fileset dir="${src}" includes="**/*.properties" />
</copy>
</target>
<target name="jars" depends="compile">
<jar jarfile="${app_jar}" basedir="${dst}" includes="com/example/**/*.*" />
</target>
Please notice that 'copy' section under the 'compile' target, it will replicate *.properties files into the build working directory. Without the 'copy' section the jar file will not contain the properties files, then you may encounter the java.util.MissingResourceException.
请注意 'compile' 目标下的 'copy' 部分,它会将 *.properties 文件复制到构建工作目录中。如果没有“copy”部分,jar 文件将不包含属性文件,那么您可能会遇到 java.util.MissingResourceException。
回答by TontonZition
With Eclipse and Windows:
使用 Eclipse 和 Windows:
you have to copy 2 files - xxxPROJECTxxx.properties - log4j.properties here : C:\Eclipse\CONTENER\TOMCAT\apache-tomcat-7\lib
你必须在这里复制 2 个文件 - xxxPROJECTxxx.properties - log4j.properties :C:\Eclipse\CONTENER\TOMCAT\apache-tomcat-7\lib
回答by Etch
I have just realized that my error was caused in the naming convention of my property file. When i used xxxx.xxxx.properties i got the error:
我刚刚意识到我的错误是由我的属性文件的命名约定引起的。当我使用 xxxx.xxxx.properties 时出现错误:
java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US
java.util.MissingResourceException: 找不到基本名称“property_file name”的包,区域设置 en_US
Changing it to something like xxx-xxxx.properties works like a charm. Hope i help someone!
将其更改为 xxx-xxxx.properties 之类的东西就像一个魅力。希望我能帮助别人!
回答by karuthiruman karthikeyan
just right click on the project file in eclipse and in build path select "Use as source folder"...It worked for me
只需右键单击 eclipse 中的项目文件并在构建路径中选择“用作源文件夹”...它对我有用
回答by Sudheer Singh
The simplest code would be like, keep your properties files into resources folder, either in src/main/resource or in src/test/resource. Then use below code to read properties files:
最简单的代码是,将您的属性文件保存在资源文件夹中,在 src/main/resource 或 src/test/resource 中。然后使用以下代码读取属性文件:
public class Utilities {
static {
rb1 = ResourceBundle.getBundle("fileNameWithoutExtension");
// do not use .properties extension
}
public static String getConfigProperties(String keyString) {
return rb1.getString(keyString);
}
}