3

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).

xlm
  • 6,854
  • 14
  • 53
  • 55
user68883
  • 828
  • 1
  • 10
  • 26
  • FYI the reason why adding locale suffix e.g. `en` or `_en_US` is unnecessary/doesn't fix your problem is because [`ResourceBundle`](https://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html) will use the file with no suffix as the default if it can't one with a matching suffix. This [answer](https://stackoverflow.com/a/48185438/885922) explains why absolute path doesn't work either. – xlm Jul 02 '20 at 00:38

4 Answers4

3

Had to include .properties files in the pom.xml:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.fxml</include>
            <include>**/*.css</include>
            <include>**/*.properties</include>
        </includes>
    </resource>
</resources>
xlm
  • 6,854
  • 14
  • 53
  • 55
user68883
  • 828
  • 1
  • 10
  • 26
  • As https://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html said, no need to define like this because maven will look for the resources under the default folder. – navylover Aug 05 '22 at 08:03
0

Do not include the .properties extension.

The ResourceBundle try to load the properties file from the current classpath.

If the properties are stored in subdirectory , use "." instead of "/".

ResourceBundle.getBundle("fontawesome.fontawesome")
  • I tried , but I still see the error, i itried both, with .properties amd without the .properties. `FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setResources(ResourceBundle.getBundle("fontawesome.fontawesome"));` `Caused by: java.util.MissingResourceException: Can't find bundle for base name fontawesome.fontawesome, locale' en_US' – user68883 Mar 14 '16 at 14:09
0

Your language properties file should end with _en, so in your case fontawesome_en.properties and you should load it with ResourceBundle.getBundle("fontawesome.fontawesome").

jansohn
  • 2,246
  • 2
  • 28
  • 40
0

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.

Tom Rutchik
  • 1,183
  • 1
  • 12
  • 15