java.util.MissingResourceException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2636521/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:11:07  来源:igfitidea点击:

java.util.MissingResourceException

javaresourcebundle

提问by user306689

I am getting below exception while running an application. This application read abc.properties file,

我在运行应用程序时遇到异常。此应用程序读取 abc.properties 文件,

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name abc, locale en_US
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:853)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:822)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:566)
    at com.ibm.dst.DailyExtract.getResourceBundle(DailyExtract.java:104)
    at com.ibm.dst.DailyExtract.main(DailyExtract.java:131)

abc.properties file reside at the workspace. I am using RSA7 as IDE, is there any setting problem? any suggestions are welcome.....

abc.properties 文件驻留在工作区。我使用 RSA7 作为 IDE,有什么设置问题吗?欢迎任何建议......

Thanks a lot in advance

非常感谢提前

回答by Alberto Zaccagni

Follow the hints in this postand see if you made one of those mistakes, which could be (copy pasted from the link):

按照这篇文章中的提示,看看你是否犯了这些错误之一,可能是(从链接复制粘贴):

  1. These resource properties files are loaded by classloader, similar to java classes. So you need to include them in your runtime classpath.

  2. These resources have fully-qualified-resource-name, similar to a fully-qualified-class-name, excerpt you can't import a resource into your java source file. Why? because its name takes the form of a string.

  3. ResourceBundle.getBundle("config")tells the classloader to load a resource named "config" with default package (that is, no package). It does NOT mean a resource in the current package that has the referencing class.

  4. ResourceBundle.getBundle("com.cheng.scrap.config")tells the classloader to load a resource named "config" with package "com.cheng.scrap." Its fully-qualified-resource-name is "com.cheng.scrap.config"

  1. 这些资源属性文件由类加载器加载,类似于java类。因此您需要将它们包含在您的运行时类路径中。

  2. 这些资源具有完全限定资源名称,类似于完全限定类名称,摘录您不能将资源导入到您的 java 源文件中。为什么?因为它的名字采用字符串的形式。

  3. ResourceBundle.getBundle("config")告诉类加载器使用默认包(即无包)加载名为“config”的资源。它并不意味着当前包中具有引用类的资源。

  4. ResourceBundle.getBundle("com.cheng.scrap.config")告诉类加载器使用包“com.cheng.scrap”加载名为“config”的资源。它的完全限定资源名称是“com.cheng.scrap.config”

回答by fish

Is the file in your classpath? If it is, try renaming it to abc_en_US.properties

该文件在您的类路径中吗?如果是,请尝试将其重命名为 abc_en_US.properties

回答by Progman

Loading the Properties file for localization stuff is also affected by the package naming. If you put your Properties file in a package like org.example.com.foobarand load it just by its name abcyou have to add the prefix org.example.com.foobar, too. If you have the properties at a different location (like in the root directory or in another subdir) you have to change either the name to load or the classpath.

加载本地化内容的属性文件也受包命名的影响。如果你把你的属性文件放在一个像这样的包中org.example.com.foobar并且只按它的名字加载它,abc你也必须添加前缀org.example.com.foobar。如果您的属性位于不同的位置(例如在根目录或另一个子目录中),您必须更改要加载的名称或类路径。

I run fine in placing the properties file in the same location where the .javafile is and using something like

我将属性文件放在文件所在的同一位置.java并使用类似的东西运行良好

private static ResourceBundle RES = ResourceBundle.getBundle(NameOfTheCurrentClass.class.getCanonicalName());

回答by Heggi

You just need to add the package name while getting the file

您只需要在获取文件时添加包名称

For example if your properties name is "ABC.properties" in package a.b.c then the below code will work perfectly

例如,如果您的属性名称是包 abc 中的“ABC.properties”,那么下面的代码将完美运行

ResourceBundle labels = ResourceBundle.getBundle("a.b.c.ABC");

回答by Stradivari

I had the same issue today and it took me a while until I figured out a fix (I'm using Eclipse as an IDE). My project folder contains the *.properties-Files in the following path:

我今天遇到了同样的问题,我花了一段时间才找到解决办法(我使用 Eclipse 作为 IDE)。我的项目文件夹包含以下路径中的 *.properties-Files:

project/src/strings

Since strings is a sub-folder of src and src is already in the build path of the project (see Property Window), all I needed was to add an Inclusion-Pattern for the *.properties-Files:

由于字符串是 src 的子文件夹,并且 src 已经在项目的构建路径中(请参阅属性窗口),我需要的只是为 *.properties-Files 添加一个包含模式:

**/*.properties

There should be already an Inclusion-Pattern for *.java-Files (**/*.java)

*.java-Files (**/*.java) 应该已经有一个包含模式

Maybe there is something similar for your IDE

也许你的 IDE 有类似的东西

回答by MesamH

Just copy the resource file over to where your class files are.

只需将资源文件复制到类文件所在的位置即可。

In my case my directory was:

就我而言,我的目录是:

bin 
  - com 
     - brookf 
        - all my packages here. 

copy your resource file to the bin folder.

将您的资源文件复制到 bin 文件夹。