簡體   English   中英

嘗試將 a.properties 文件加載到資源包中時出現錯誤“找不到基本名稱的包”

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

我正在嘗試將.properties文件加載到我的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);
    }
}

但我收到錯誤:

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

在此處輸入圖像描述

不知道為什么。 我已經多次檢查路徑/sample/resources/language_en.properties是否存在拼寫錯誤。 我也嘗試過重建項目(使用 IntelliJ)。

有誰知道為什么?

ResourceBundle.getBundle()方法采用資源包的基本名稱,一個完全限定的 class 名稱(由 class 文檔指定)。 你可以像這樣加載你的包:

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

要使用英文ResourceBundle ,您可以指定Locale

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

更多信息請參考本教程: 自定義資源包加載

加載資源包時,基本名稱擴展名中沒有.properties 此外,刪除基本名稱的前導/

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM