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:

  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"

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:

  1. 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.
  2. 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.

转:解决 java.util.MissingResourceException: Can't find bundle for base name com...config, locale zh_CN 错误的更多相关文章

  1. java.util.MissingResourceException: Can't find bundle for base name init, locale zh_CN问题的处理

    一.问题描述 项目开发使用的是SSM框架,项目那个正常运行,开发一个新功能后,添加了一些配置文件,再重新运行项目抛出异常,找不到name为init的bean. 二.异常信息详细 六月 30, 2018 ...

  2. java.util.MissingResourceException: Can't find bundle for base name db, locale zh_CN

    在使用Bundle来加载配置文件的时候, 爆出了这个错误: 原因? 没有找到需要加载的配置文件,因为配置文件必须放在src目录下面, 如果放进了com.bj186.crm的包下面,就必须添加包的名称到 ...

  3. Caused by: java.util.MissingResourceException: Can't find bundle for base name javax.servlet.LocalStrings, locale zh_CN

    这个是很早以前的一个bug了,最近开始用idea发现追源码相当方便,于是结合网上的解决方案以及自己的判断追踪一下原因,当然没有深究,只是根据提示一直追而已:先说一下解决方案: <dependen ...

  4. Java: MissingResourceException, "Can't find bundle for base name myMessage, locale zh_CN"

    在java中,在多语言国际化时可以用 *.java 类来作为资源文件使用. 1. 首先定义类, 类必须继承ListResourceBundle 类所在路径为: src/I18N public clas ...

  5. java.util.MissingResourceException: Can't find resource for bundle oracle.sysman.db.rsc.LoginResourc

    http://blog.itpub.net/197458/viewspace-1055358/   oracle 10.2.0.4 windows 2003 X64 平台 系统安装EMCA正常.第一次 ...

  6. java读取package中的properties文件java.util.MissingResourceException

    文件结构: /build/classes/d914/Hello.class /build/classes/d914/mess.properties /build/classes/d914/mess_z ...

  7. 谈论高并发(二十二)解决java.util.concurrent各种组件(四) 深入了解AQS(二)

    上一页介绍AQS其基本设计思路以及两个内部类Node和ConditionObject实现 聊聊高并发(二十一)解析java.util.concurrent各个组件(三) 深入理解AQS(一) 这篇说一 ...

  8. (转)p解决 java.util.prefs.BackingStoreException 报错问题

    原文:https://blog.csdn.net/baidu_32739019/article/details/78405444 https://developer.ibm.com/answers/q ...

  9. spark 解决 java.util.Date is not a valid external type for schema of Date

    出错伪代码如下: //出错的点在这里 import java.util.Date ... val t_rdd = t_frame.rdd.map(row => { val photo_url = ...

随机推荐

  1. gpio子系统和pinctrl子系统(上)

    前言 随着内核的发展,linux驱动框架在不断的变化.很早很早以前,出现了gpio子系统,后来又出现了pinctrl子系统.在网上很难看到一篇讲解这类子系统的文章.就拿gpio操作来说吧,很多时候都是 ...

  2. jQuery重置单选框和input框

    取消选中单选框radio,第一种,第二种方式是使用jQuery实现的,第三种方式是基于JS和DOM实现的,大家可以看看下面的示例 本文提供了三种取消选中radio的方式,代码示例如下: 本文依赖于jQ ...

  3. JavaScript阻止修改对象的三种方式

    JavaScript中,我们希望别人无法修改我们创建的对象.比如,代码库的作者很可能想锁定核心库的某些部分来保证它们不被意外地修改.ES5中引入了三种锁定修改的级别:防止扩展preventExtens ...

  4. MYSQL数据库的数据完整性

    #转载请联系 数据库中存储的数据应该符合我们的预期, 这就是数据完整性. 那么如何实现数据完整性? 我们通过以下两方面来实现数据的完整性: 数据类型: 存储在数据库中的所有数据值均正确的状态.如果数据 ...

  5. Nginx网站根目录更改及导致403 forbidden的问题解决

    最近因为工作需要,要将Nginx网站根目录更改下,通过网上的一些教程更改后,但发现测试的时候一直提示403 forbidden错误,后台通过一个朋友的提示也解决了,所以现在将详细的步骤分享给大家,有需 ...

  6. yii2中判断数据表是否存在数据库中(原创)

    分为两步: 第一步,找出数据库中所有表名,表名得到的是二维数组. 第二步,判断表名是否存在二维数组中 下面就贴我的代码咯. $table_name =‘table’; $juge = $handle- ...

  7. [BZOJ2151] 种树 贪心

    2151: 种树 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1151  Solved: 613[Submit][Status][Discuss] ...

  8. LPD Office插件使用指南

    LPD Office插件已经发布至Azure上,您可以在本机Outlook和Office Online使用该插件 一:在Outlook中使用 LPD Office插件 打开Outlook应用,并点击“ ...

  9. HSV与RGB的相互转换的公式

    H参数表示色彩信息,即所处的光谱颜色的位置.该参数用一角度量来表示,红.绿.蓝分别相隔120度.互补色分别相差180度.纯度S为一比例值,范围从0到1,它表示成所选颜色的纯度和该颜色最大的纯度之间的比 ...

  10. 随机数问题--已知有个Random7()的函数,返回1到7随机自然数,让利用这个Random7()构造Random10()随机1~10.

    Math.random()随机生成(0,1)之间的float数,Random7随机生成[1,7]之间的整数,利用Random7构造Random10的步骤: 1.生成数a:a是两次Random7的结果, ...