Aaron
Aaron

Reputation: 846

ResourceBundle can't be found in jar

I have a problem when I export my project to a jar. The ResourceBundles work fine when I run the project in Eclipse, but not from the jar. I exported it as Export -> jar file.

I start the jar in (Windows) cmd -> java -jar %projectName%.jar. The ResourceBundles are:

And I load it this way:

ResourceBundle.getBundle("language.Language", currentLocale)

I looked at so many other questions about this on Stackoverflow and their problem was always that they didn't provide a path from the root. I have no idea why this isn't working. I tried putting these .properties files at root too...

Note: the class that manages these bundles/locales is also in the same packages.

Thanks in advance.

Edit: maybe some extra information, I get the Strings this way:

public static String getString(String key) {
        try {
            return ResourceBundle.getBundle("language.Language", currentLocale)
                    .getString(key);
        } catch (MissingResourceException e) {
            return "KEY " + key + " NOT FOUND !";
        }
}

Stacktrace:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle
 for base name language.Language, locale nl_be
        at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
        at java.util.ResourceBundle.getBundleImpl(Unknown Source)
        at java.util.ResourceBundle.getBundle(Unknown Source)
        at language.LanguageModel.getString(LanguageModel.java:33)
        at cli.class.method(class.java:35)
        at main.Main.main(Main.java:26)

The properties:

Language_en_US.properties
Language_fr_FR.properties
Language_nl_BE.properties
....

Upvotes: 2

Views: 3953

Answers (1)

JB Nizet
JB Nizet

Reputation: 691765

The locale you pass in argument to ResourceBundle.getBundle() has its country set to "be". It should be set to "BE" instead.

You should also consider using properties files with only the langage code suffix, and have one backup bundle without suffix at all:

  • Language : will be used at last resort
  • Language_nl : will be used when the language is "nl"
  • Language_nl_BE : should override the translations from Language_nl if they're specific to Belgium

Upvotes: 3

Related Questions