Microsoft KB Archive/327329

= BUG: Cannot Access Properties File with ResourceBundle =

Article ID: 327329

Article Last Modified on 1/19/2004

-

APPLIES TO


 * Microsoft Visual J# .NET 2003 Standard Edition

-



This article was previously published under Q327329



SYMPTOMS
When you use the getBundle method from java.util.ResourceBundle to access a properties file, you may receive the following exception error message:

An unhandled exception of type 'java.util.MissingResourceException' occurred in vjslib.dll



CAUSE
This is a bug in ResourceBundle when it tries to load the resources. The calling assembly name is being passed incorrectly.



RESOLUTION
There are two possible workarounds for this problem.   Use the PropertyResourceBundle class to manage the resources. PropertyResourceBundle requires an InputStream. Therefore, for localized resources, you must calculate the localized name of the resource. For example, replace the code String name = &quot;MyResources&quot;; ResourceBundle propertyBundle = ResourceBundle.getBundle(name, locale); with the following: String name = &quot;MyResources&quot;; if (!Locale.ENGLISH.equals(locale)) name += &quot;_&quot; + locale; // Load resource as inputStream. java.io.InputStream is = testProperties.class.getResourceAsStream(name+&quot;.properties&quot;); // Construct PropertyResourceBundle PropertyResourceBundle propertyBundle = new PropertyResourceBundle(is); // Here, if you give a ResourceBundle Object, you cannot gain access to the handleGetObject method // because it is protected in ResourceBundle while it is public in PropertyResourceBundle. // You must have access to this method.   Provide a ResourceBundle subclass that manages your resources. Instead of using an external property file, create a class that inherits from ResourceBundle. The following subclass example contains the bundle named MyResources. For localized resources, there are additional subclasses that are named by using the extension of the locale. For example, the German subclass is named MyResources_de. public class MyResources extends ResourceBundle {    public Object handleGetObject(String key) {        if (key.equals(&quot;s1&quot;)) return &quot;String1&quot;; if (key.equals(&quot;s2&quot;)) return &quot;String2&quot;; return null; } }                   



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
Note that in the first workaround, you also do not see a fallback behavior to a default locale if the properties file does not exist. You must add custom handler code for a default resource file if there is a possibility that the desired locale-specific properties file does not exist.

