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 01: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 02: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 06:57 AM 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 17, 2009 at 10:47 PM EST #

zxz

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

sf

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

That was a very good answer. Thanks a lot

Posted by Yashwanth on October 20, 2009 at 11:06 PM EDT #

asdsadsa

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

nice tips. thanks for helping me to resolve MissingResourceException :)

Posted by bhat on December 30, 2009 at 05:31 PM EST #

Thnx a lot. This post is very helpful !

Posted by Opio on January 21, 2010 at 06:09 AM EST #

good one.. it helps me solving my problem

Posted by preetham on February 18, 2010 at 11:47 PM EST #

Thank you! Your article helped me a lot!

Posted by Randy Gupta on March 11, 2010 at 11:31 PM EST #

Thank you very much! You are the first who really show how to use resources in java, your words about being baffled are so true!

Posted by Sergey on May 09, 2010 at 07:06 AM EDT #

Nice one. First site I googled with a decent explanation.

Posted by debacke on December 06, 2010 at 10:03 PM EST #

you made my day :)
thanks a lot

Posted by Talaat Safwat on December 16, 2010 at 09:10 PM EST #

thank you for the help

Posted by guest on February 02, 2011 at 06:38 AM EST #

What about back-ward compatibility with previous versions of weblogic. Why did weblogic suddenly decide to change the way the properties files are loaded. We are migrating our application from weblogic 9 to weblogic 10 and the application does not work because the properties files cannot be read.

Posted by weblogic user on February 22, 2011 at 10:35 AM EST #

It is really helpful.

Posted by guest on March 09, 2012 at 03:04 AM EST #

i set the fully classpath like com.app.properties.ApplicationProperties, eventhough i m getting the same error. Can u plz help me out?

Posted by kumar on March 10, 2012 at 02:16 AM EST #

package com.uks.pallav.core.day6.assignment4;
import java.util.*;

public class ResourceBundleExample {

public static void main(String args[]) {
try {
Locale[] locales = new Locale[]{
new Locale("fr", "FR"), new Locale("en", "IN")
};
ResourceBundle bundle = null;
for (int i = 0; i < locales.length; i++) {
bundle = ResourceBundle.getBundle("testfile", locales[i]);

System.out.println(locales[i].getDisplayName());
System.out.println(bundle.getString("Hello"));
System.out.println(bundle.getString("Goodbye")+"\n");
}

} catch (Exception e) {
System.out.println(e);
}
}
}

Can anyone tell me why "bundle = ResourceBundle.getBundle("testfile", locales[i]);" is still giving "java.util.MissingResourceException" whicle my properties file is in Sorurce

Posted by guest on March 27, 2012 at 08:38 PM EDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Cheng Fang

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today