Unable to load ResourceBundle with ResourceBundle.getBundle() Can't find bundle for base name , locale en_US

Viewed 7356

I see the following exception when I try to load a properties file:

Caused by: java.util.MissingResourceException: Can't find bundle for base name /fontawesome/fontawesome, locale en_US

I'm using a maven project and my properties file is located at src\main\resources\fontawesome\fontawesome.properties

I'm using the below code to load this file from JavaFX8 main class:

FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setResources(ResourceBundle.getBundle("/fontawesome/fontawesome.properties"));

Trying the absolute path fails, as does renaming the file to fontawesome_en_US.properties or fontawesome_en.properties (as suggested in other SO posts).

4 Answers

Here's my experience: If you put a "." in the name then the ResourceBundle.getBundle() will look for a class instead of properties file. That class will look something like this:

import java.util.ListResourceBundle;

public class DataEncryptionStrings extends ListResourceBundle {
@Override
protected Object[][] getContents() {
    return new Object[][] {
        {"ErrorCreateKeystoreInstance", "an exception occurred creating the keystore instance"},
        {"ErrorLoadInitialKeystore", "an exception occurred loading the initial keystore"},
        {"ErrorLoadKeystore", "an exception occurred loading the keystore"},

It follows the naming convention:
{classname}.class is the default class
{classname}_en_US.class is english US class
{classname}_fr_FR.class in the french class

if you don't have any "." in the name but have a "/" in it, then it'll look for property files instead.

If you don't have either a "." of "/" in the name, I think either property files or ListResourceBundle can be made available. It'll find whatever you've provided. I'm not sure though if that behavior is same for all VMs.

The problem I had that got me interested in searching for a solution was that I had a filename with "."'s in it. I'm pretty sure that my problem was that I implement properties file and not ListResourceBundle classes, and with a file name containing a "." in it, ResourceBundle.getBundle(name) looked for ListResourceBundle classes. When I replaced the "." in the filename with a "_", I was then able to find the default ResourceBundle properties file.

User68883 seems to be using an absolute address "/fontawesome/fontawesome.properties" instead of a relative address "fontawesome/fontawesome.properties". Also, on the ResourceBundle.getBundle, never include the files extension in the name nor the Locale. That's what ResouceBundle automagically does for you as it goes through a resolution process to get the best resource you have in the resource directory for the devices Locale.

Related