Search code examples
javaresourcebundle

ResourceBundle example not working


I'm trying the example of "Learning Java" about RersourceBundles.

I'm using Windows 7 and NetBeans

The code of the sample is:

import java.util.*;
public class Hello {
    public static void main(String[] args) {
        ResourceBundle bun;
        bun = ResourceBundle.getBundle("Message", Locale.ITALY);
        System.out.println(bun.getString("HelloMessage"));
        bun = ResourceBundle.getBundle("Message", Locale.US);
        System.out.println(bun.getString("HelloMessage"));
    }
}

If I run the code I get:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name Message, locale it_IT at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499) at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322) at java.util.ResourceBundle.getBundle(ResourceBundle.java:795) at learningjava4.Hello.main(Hello.java:14) Java Result: 1

Even if I wrote and save

import java.util.*;
public class Message_it_IT extends ListResourceBundle {
    public Object[][] getContents() {
        return contents;
    }
    static final Object[][] contents = {
        {"HelloMessage", "Buon giorno, world!"},
        {"OtherMessage", "Ciao."},
    };
}

I still had the same error. The file Message_it_IT was saved on the same directory of the other one, what is wrong?

Thanks


Solution

  • The stack trace shows that the class is in the package learningjava4. And you're telling us that the bundle class/files are in the same package. So the bundle base name is learningjava4.Message, not Message.