Search code examples
javapropertiesresourcesresourcebundle

Can't find resource bundle exception


I want to use a resource bundle called strings but I get following error when running my main method in MainApplication.java:

java.util.MissingResourceException: Can't find bundle for base name strings, locale de_DE
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:845)
    at logic.MainApplication.start(MainApplication.java:20) (...)

My project structure:

project structure

The two files strings.properties and strings_de_DE.properties are stored in the same directory as MainApplication.java and all three files are in the directory pi_display/src/logic/ (IntelliJ puts them into a virtual "Resource Bundle" directory for a clearer display of the project structure).

Main class/method:

public class MainApplication {
    public static void main(String[] args) {
        Locale locale = Locale.GERMANY;
        ResourceBundle resources = ResourceBundle.getBundle("strings", locale);
        // ...
    }

I've already tried to move the files to other packages and specify the baseName of the bundle (i.e. logic.strings or other package structures if I moved them elsewhere).

I don't understand why the bundle can't be found given MainApplication and the bundle files are in the same directory.


Solution

  • You probably using wrong baseName for your bundle. According to documentation:

    Gets a resource bundle using the specified base name, the default locale, and the caller's class loader

    "Class loader" means that resource loaded from folder with compiled classes. In the screenshot, the path to your resource is src/logic/strings.properties, so it should be located in classes/logic/ folder, and as baseName you should use logic/strings (or logic/strings.properties, I'm not sure about that).