Java 无法使用ResourceBundle加载ResourceBundle。getBundle()可以';找不到基本名称、语言环境的捆绑包

Java 无法使用ResourceBundle加载ResourceBundle。getBundle()可以';找不到基本名称、语言环境的捆绑包,java,javafx-8,maven-3,resourcebundle,fxmlloader,Java,Javafx 8,Maven 3,Resourcebundle,Fxmlloader,我在尝试加载属性文件时看到以下异常: Caused by: java.util.MissingResourceException: Can't find bundle for base name /fontawesome/fontawesome, locale en_US 我正在使用一个maven项目,我的属性文件位于src\main\resources\fontsawesome\fontsawesome.properties 我使用以下代码从JavaFX8主类加载此文件: FXMLLoade

我在尝试加载属性文件时看到以下异常:

Caused by: java.util.MissingResourceException: Can't find bundle for base name /fontawesome/fontawesome, locale en_US
我正在使用一个maven项目,我的属性文件位于
src\main\resources\fontsawesome\fontsawesome.properties

我使用以下代码从JavaFX8主类加载此文件:

FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setResources(ResourceBundle.getBundle("/fontawesome/fontawesome.properties"));

尝试绝对路径失败,将文件重命名为
fontawesome_en_US.properties
fontawesome_en.properties
(如其他SO帖子所建议的那样)。

不包括.properties扩展名

ResourceBundle尝试从当前类路径加载属性文件

如果属性存储在子目录中,请使用“.”而不是“/”


您的语言属性文件应以\u en结尾,因此在您的情况下
fontawesome\u en.properties
ResourceBundle.getBundle(“fontawesome.fontawesome”)
必须在
pom.xml
中包含
.properties
文件:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.fxml</include>
            <include>**/*.css</include>
            <include>**/*.properties</include>
        </includes>
    </resource>
</resources>

src/main/resources
**/*.fxml
**/*.css
**/*.物业
以下是我的经验: 如果在名称中添加“.”,则ResourceBundle.getBundle()将查找类而不是属性文件。该类将如下所示:

导入java.util.ListResourceBundle

public class DataEncryptionStrings extends ListResourceBundle {
@Override
protected Object[][] getContents() {
    return new Object[][] {
        {"ErrorCreateKeystoreInstance", "an exception occurred creating the keystore instance"},
        {"ErrorLoadInitialKeystore", "an exception occurred loading the initial keystore"},
        {"ErrorLoadKeystore", "an exception occurred loading the keystore"},
它遵循命名约定:
{classname}.class是默认类
{classname}{u en_US.class是英语美国class
{classname}_fr_fr.class在法语类中

如果名称中没有“.”但有“/”,则它将查找属性文件。

如果名称中没有“.”of“/”,我认为可以使用属性文件或ListResourceBundle。它会找到你提供的任何东西。但我不确定所有虚拟机的行为是否相同。

我对搜索解决方案感兴趣的问题是,我的文件名中有“.”。我很确定我的问题是我实现了属性文件而不是ListResourceBundle类,并且在文件名中包含“.”时,ResourceBundle.getBundle(name)查找ListResourceBundle类。当我将文件名中的“.”替换为“\”时,我就能够找到默认的ResourceBundle属性文件


User68883似乎使用的是绝对地址“/fontawesome/fontawesome.properties”,而不是相对地址“fontawesome/fontawesome.properties”。另外,在ResourceBundle.getBundle上,不要在名称或区域设置中包含文件扩展名。这就是ResourceBundle自动为您所做的,因为它通过解析过程来获取设备区域设置的资源目录中的最佳资源。

我尝试过,但仍然看到错误,我尝试了这两种方法,使用.properties和不使用.properties。
fxmloader fxmloader=new fxmloader();setResources(ResourceBundle.getBundle(“fontawesome.fontawesome”)`由以下原因引起:java.util.MissingResourceException:找不到基名称fontawesome.fontawesome的捆绑包,区域设置“en_US”仅供参考添加区域设置后缀(例如
en
\u en_US
是不必要的/无法解决您的问题的原因是,如果无法使用具有匹配后缀的文件,则将不带后缀的文件作为默认文件。这也解释了为什么绝对路径也不起作用。
public class DataEncryptionStrings extends ListResourceBundle {
@Override
protected Object[][] getContents() {
    return new Object[][] {
        {"ErrorCreateKeystoreInstance", "an exception occurred creating the keystore instance"},
        {"ErrorLoadInitialKeystore", "an exception occurred loading the initial keystore"},
        {"ErrorLoadKeystore", "an exception occurred loading the keystore"},