Skip to content
Advertisement

Java could not find XML resource bundle

src/java
src/resource/foo/messages.xml
                 messages_fr.xml

ResourceBundle bundle = ResourceBundle.getBundle("foo.messages", Locale.ENGLISH);

error:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name foo.messages, locale en
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1581)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1396)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:854)

If they are *.properties, no problems.

How to load XML resource bundles?

Advertisement

Answer

By default, Java SE ResourceBundle.getBundle(...) only understands loading of resource bundles implemented as Java classes or “.properties” files.

If you want to load a ResourceBundle from an XML file, you need to provide a custom ResourceBundle.Control implementation that implements the resource search and loading strategies.

There is an example in the ResourceBundle.Control javadocs; look for “Example 2”.


I briefly searched for a 3rd-party implementation of an XML ResourceBundle provider, but I couldn’t find anything generic.

(Asking recommendations is off-topic anyway.)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement