`
dfwang
  • 浏览: 97560 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

转:解决 java.util.MissingResourceException: Can't find bundle for base name com...c

    博客分类:
  • j2ee
阅读更多
转:解决 java.util.MissingResourceException: Can't find bundle for base name com...config, locale zh_CN 错误

Solve java.util.MissingResourceException: Can't find bundle for base name com...config, locale zh_CN
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:836)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:805)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:576)


You know java is looking for a properties file in a specific locale.  You may be baffled why java keeps complaining it can't find a properties file that is right there.  A few things to keep in mind when debugging this type of errors:

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

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.


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.

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"
For instance, you have a project like


C:\ws\netbeans5\scrap>
|   build.xml
+---build
|   \---classes
|       \---com
|           \---cheng
|               \---scrap
|                       Scrap.class
|
+---src
|   \---com
|       \---cheng
|           \---scrap
|                   config.properties
|                   Scrap.java



For this statement in Scrap.java: ResourceBundle config = ResourceBundle.getBundle("config"); to work, you will need to  cp src\com\cheng\scrap\config.properties build\classes\ such that config.properties is directly under classes, and at the same level as com.  Alternatively, you can put config.properties into a config.jar such that config.properties is at the root of config.jar without any subdirectories, and include config.jar in the classpath.


For this statement in Scrap.java: ResourceBundle config = ResourceBundle.getBundle("com.cheng.scrap.config"); to work, you will need to  cp src\com\cheng\scrap\config.properties build\classes\com\cheng\scrap\ such that config.properties is directly under classes\com\cheng\scrap\, and at the same level as scrap.  Alternatively, you can put com\cheng\scrap\config.properties (along with the long subdirectories) into a config.jar, and include config.jar in the classpath.  

You may be wondering why it is made so confusing?  The benefits are two-fold, as I see it: 

Location transparency.  At runtime, config.properties is NOT a file, it's just a a loadable resource.  config.properites may not exist in your project at all, and the person who wrote Scrap.java may have never seen this resource.  A URLClassLoader can find it in a network path or URL at runtime.  This is especially important for server-side components such as EJB, Servlet, JSP, etc, who are normally not allowed to access file systems.  When you ask classloaders for a resource, its physical location becomes irrelevant.

Namespace mechanism.  Having a package allows multiple packages to have resources with the same short name without causing conflicts. This is no different from java packages and xml namespaces.


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/nickshen3/archive/2007/07/19/1698261.aspx
分享到:
评论

相关推荐

    JAVA中关于String的一些注意点

    ### JAVA中关于String的一些注意点 在Java编程语言中,`String` 类是最常用的数据类型之一,用于处理文本数据。本文将深入探讨Java中的`String`类及其使用时需要注意的关键点,希望对开发者们有所帮助。 #### 1. ...

    datax-v202309 clickhousereader bug修复后jar包 免费下载

    datax_v202309 默认的clickhousereader 报错:java.util.MissingResourceException: Can't find bundle for base name com.alibaba.datax.plugin.reader.clickhousereader.LocalStrings, locale zh_CN。 该资源为BUG...

    Java中常见错误和异常解决方法(打印).doc

    #### 六、`Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name Message, locale zh_CN` **原因**:在进行国际化操作时,如果无法找到指定的资源文件(如属性文件)...

    Maven2 的新特性.7z

    java.util.MissingResourceException: Can't find bundle for base name pmd-report, locale zh_CN at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle .java:839) at java.util....

Global site tag (gtag.js) - Google Analytics