MissingResouceException even when the resource exists

ResourceBundle.getBundle() throws MissingResourceException when resource files are available and were created at runtime just before the call.

The method works the second time around when the files are already available.

Here is a sample program that does just that. The program creates a dummy .properties file and then calls

ResourceBundle.getBundle()

Test Program:

import java.util.ResourceBundle;

import java.util.Locale;

import java.io.File;

import java.io.FileWriter;

import java.io.FileReader;

class DynResTest {

public void dynResourceTest() throws Exception {

File f = new File("dynresources");

if (!f.isDirectory()) {

if (f.mkdir()) {

FileWriter fileWriter = new FileWriter("dynresources/dynres.properties");

String line = "key=value\n";

fileWriter.write(line, 0, line.length());

fileWriter.flush();

fileWriter.close();

System.out.println("Created dynresources/dynres.properties");

}

}

FileReader fileReader = new FileReader("dynresources/dynres.properties");

char[] buf = new char[512];

int read = fileReader.read(buf);

System.out.println("\n<-dynresources/dynres.properties contains->");

String s = new String(buf, 0, read);

System.out.println(s);

Locale defaultLocale = Locale.getDefault();

String os = System.getProperty( "os.name" );

String variant = "";

if ( os.startsWith( "Mac" ) )

{

variant = "mac";

}

else if ( os.startsWith( "Win" ) )

{

variant = "win";

}

Locale locale = new Locale( defaultLocale.getLanguage(), defaultLocale.getCountry(), variant );

System.out.println("Resource is "+ResourceBundle.getBundle("dynres", locale));

}

public static void main(String[] args) {

DynResTest t = new DynResTest();

try {

t.dynResourceTest();

}

catch(Exception e) {

e.printStackTrace();

}

}

}

Output:

C:\test\testj>java -cp ".;./dynresources" DynResTest

Created dynresources/dynres.properties

<-dynresources/dynres.properties contains->

key=value

java.util.MissingResourceException: Can't find bundle for base name dynres, locale en_US_WIN

at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)

at java.util.ResourceBundle.getBundleImpl(Unknown Source)

at java.util.ResourceBundle.getBundle(Unknown Source)

at DynResTest.dynResourceTest(DynResTest.java:42)

at DynResTest.main(DynResTest.java:48)

C:\test\testj>java -cp ".;./dynresources" DynResTest

<-dynresources/dynres.properties contains->

key=value

Resource is java.util.PropertyResourceBundle@2e000d

C:\test\testj>

[2879 byte] By [kurienmathew] at [2007-9-26 4:41:08]
# 1

You scared me with this sample. For a very short moment I thought that using all this static methods (getProperty, getDefaultLocale, getResourceBundle) had fooled the JVM into some optimization one does not want... So, I ran your code to make sure I should not start using Visual Basic :-)

I have exact the same behavior when repeating your steps. However, everything runs fine when I get rid of the dynresources subdirectory. So, if I create the file in the very same directory as my class file, then the code behaves as expected.

I guess the JVM checks the -cp parameter when starting up. When you start your program, the dynsources directory does not yet exist, and the JVM remembers that for this "run session" -- and does not check it at a later time. Note that System.getProperty("java.class.path") simply returns the thing you defined -- whether or not the directories exist.

You could verify this if you create an empty dynsources directory (and change your code a bit). Alternatively, simply put the dynamic file into the directory that stores the classes.

a.

avbentem at 2007-6-29 18:03:14 > top of Java-index,Desktop,I18N...
# 2

> created at runtime just before the call

Apart from your problem: do you know that resource bundles maybe cached? So, make sure that your dynamic resource bundle name is always different, as otherwise there is no way to force the resource to be reloaded.

I did some testing, and ResourceBundle seems to cache a specifc bundle at least as long as a reference to it (as returned by getBundle()) exists. But even if that reference is released, getBundle() may still return a cached bundle -- even if your resource has changed on the file system!

http://developer.java.sun.com/developer/bugParade/bugs/4212439.html

a.

avbentem at 2007-6-29 18:03:14 > top of Java-index,Desktop,I18N...
# 3

Hi All

im new to Internationalization.

Can anybody tell me how to Read Chinese characters thru ReaourceBundle class.

Please tell me the settings, fonts... i need to have for this.

This is so urgent. Pls help me ASAP.

Thanx in advance.

Thnx,

Sabari

aldoblas1 at 2007-6-29 18:03:14 > top of Java-index,Desktop,I18N...