简体   繁体   中英

Trying to load a .properties file into Resource bundle getting error "Can't find bundle for base name"

I'm trying to load a .properties file into my ResourceBundle

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception{

        ResourceBundle bundle = ResourceBundle.getBundle("/sample/resources/language_en.properties");

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/sample/layout/test_form.fxml"), bundle);
    }
}

But I'm getting the error:

Caused by: java.util.MissingResourceException: Can't find bundle for base name /sample/resources/language_en.properties, locale en_US

在此处输入图像描述

Can't figure out why. I've checked the path multiple times /sample/resources/language_en.properties for spelling mistakes. I've also tried rebuilding the project (Using IntelliJ).

Does anyone have an idea why?

The method ResourceBundle.getBundle() takes the base name of the resource bundle, a fully qualified class name (as specified by the class documentation). You can load your bundle like this:

// this will load the base bundle: sample/resources/language.properties
ResourceBundle bundle = ResourceBundle.getBundle("sample.resources.language");

To use the English ResourceBundle , you can specify a Locale :

// this will load sample/resources/language_en.properties
ResourceBundle bundle = ResourceBundle.getBundle("sample.resources.language", Locale.ENGLISH);

Please refer to this tutorial for more information: Customizing Resource Bundle Loading

When loading resource bundle don't have .properties in basename extensions. Also, remove the leading / for the basename.

Use ResourceBundle bundle = ResourceBundle.getBundle("sample/resources/language_en");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM