A Sunny Commune - Cheng's Blog

« No more ({ }) for... | Main | AJAX模式在小学考试中的应用 »

http://blogs.sun.com/chengfang/date/20060406 Thursday April 06, 2006

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)

(Here is another blog on how to debug this exception, but here is what I will do.)

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.

 

technorati tags: , ,

Comments:

Hi,
thanks, this post helped me a lot it gave some useful information for me.One thing I have a doubt, exactly the same problem I got but at the debugging time I gave full absolute path even also there is no positive result.

Posted by J.Naveen on May 19, 2008 at 08:05 PM EDT #

Hi,

I have a peculiar problem.. I am not getting this error, when the server is started.. it is working fine for sometime.
but as the page is used for a longer time, it throws me this error.
i hope there might be any database connection problems. But not sure whether it is the reason. Can anyone give an insight into this?

Posted by anandraj on May 26, 2008 at 09:43 AM EDT #

Gr8 way Just one sentence solved the problem... "resource properties files are loaded by classloader"... Thanks a lot. For explanation....

Posted by Kunal Handa on July 17, 2008 at 01:57 PM EDT #

I cannot find .properties files . I don't know how to create this properties files and include it in my project.

Posted by rahul ranjan on February 18, 2009 at 06:47 AM EST #

zxz

Posted by cxdsd on March 11, 2009 at 06:28 PM EDT #

sf

Posted by 202.91.140.4 on September 09, 2009 at 09:15 AM EDT #

That was a very good answer. Thanks a lot

Posted by Yashwanth on October 21, 2009 at 06:06 AM EDT #

asdsadsa

Posted by asdsad on November 11, 2009 at 08:32 AM EST #

Post a Comment:
  • HTML Syntax: NOT allowed