Can't find resource bundle exception

江枫思渺然 提交于 2020-07-08 20:34:13

问题


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:

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.


回答1:


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



来源:https://stackoverflow.com/questions/48184900/cant-find-resource-bundle-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!