简体   繁体   English

java.util.MissingResourceException

[英]java.util.MissingResourceException

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. abc.properties文件驻留在工作区中。 I am using RSA7 as IDE, is there any setting problem? 我使用RSA7作为IDE,有任何设置问题吗? any suggestions are welcome..... 欢迎任何建议.....

Thanks a lot in advance 非常感谢提前

Follow the hints in this post and 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. 这些资源属性文件由类加载器加载,类似于java类。 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. 这些资源具有完全限定资源名称,类似于完全限定类名称,摘录您无法将资源导入Java源文件。 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). ResourceBundle.getBundle("config")告诉类加载器使用默认包(即没有包)加载名为“config”的资源。 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." ResourceBundle.getBundle("com.cheng.scrap.config")告诉类加载器使用包“com.cheng.scrap”加载名为“config”的资源。 Its fully-qualified-resource-name is "com.cheng.scrap.config" 它的完全限定资源名称是“com.cheng.scrap.config”

You just need to add the package name while getting the file 您只需在获取文件时添加包名称即可

For example if your properties name is "ABC.properties" in package abc then the below code will work perfectly 例如,如果您的属性名称是包abc中的“ABC.properties”,则下面的代码将完美地运行

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

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文件夹。

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.foobar and load it just by its name abc you have to add the prefix org.example.com.foobar , too. 如果将属性文件放在像org.example.com.foobar这样的包中并仅通过其名称abc加载它,则还必须添加前缀org.example.com.foobar 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. 如果您具有不同位置的属性(例如在根目录或其他子目录中),则必须更改要加载的名称或类路径。

I run fine in placing the properties file in the same location where the .java file is and using something like 我运行良好的属性文件放在.java文件所在的位置,并使用类似的东西

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

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). 我今天遇到了同样的问题,我花了一段时间才找到修复程序(我使用Eclipse作为IDE)。 My project folder contains the *.properties-Files in the following path: 我的项目文件夹包含以下路径中的* .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: 由于strings是src的子文件夹,src已经在项目的构建路径中(参见Property Window),我所需要的只是为* .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有类似的东西

Is the file in your classpath? 该文件是否在您的类路径中? If it is, try renaming it to abc_en_US.properties 如果是,请尝试将其重命名为abc_en_US.properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM