java.util.MissingResourceException:找不到基本名称的包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3383191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:47:35  来源:igfitidea点击:

java.util.MissingResourceException: Can't find bundle for base name

javainternationalizationresourcebundle

提问by Gabriel A. Zorrilla

I'm testing Java's i18n features and have a problem, I can't load the language file when it's not in the class root. Right now my files are in the /langdirectory.

我正在测试 Java 的 i18n 功能并遇到一个问题,当语言文件不在类根目录中时,我无法加载它。现在我的文件在/lang目录中。

Looked several answers here in SO, putting it in a classessubdir and loading it like lang.Messages, used complete location routing /Test/lang/Message(test is the project name), using just /lang/Messageand still I'm getting the:

在 SO 中查看了几个答案,将其放在classes子目录中并加载它lang.Messages,使用完整的位置路由/Test/lang/Message(测试是项目名称),仅使用/lang/Message并且仍然得到:

java.util.MissingResourceException: Can't find bundle for base name

java.util.MissingResourceException: Can't find bundle for base name

error.

错误。

Anything else to try?

还有什么可以尝试的吗?

My file structure is:

我的文件结构是:

Test/lang/Messages_es.properties

Test/src/test/Main.java

Test/lang/Messages_es.properties

Test/src/test/Main.java

import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JFrame;

public class Main {

     public static void main(String[] args) {

    Locale currentLocale;
    ResourceBundle messages;

    currentLocale = new Locale("es");

    messages = ResourceBundle.getBundle("Messages", currentLocale);
    System.out.println(messages.getString("Messagesgreetings"));
    System.out.println(messages.getString("Messagesinquiry"));
    System.out.println(messages.getString("Messagesfarewell"));
    }
}

采纳答案by Marimuthu Madasamy

You need to have your locale name in your properties file name.

您需要在属性文件名中包含您的语言环境名称。

Rename your properties file to Messages_es.properties

将您的属性文件重命名为 Messages_es.properties

Since you haven't declared any package, both your compiled class file and the properties file can be in the same root directory.

由于您尚未声明任何包,因此您编译的类文件和属性文件可以位于同一根目录中。

EDIT in response to comments:

编辑以回应评论:

Lets say you have this project structure:

假设你有这个项目结构:

test\src\foo\Main.java(foois the package name)

test\src\foo\Main.javafoo是包名)

test\bin\foo\Main.class

test\bin\resources\Messages_es.properties(properties file is in the folder resourcesin your classpath)

test\bin\resources\Messages_es.properties(属性文件resources位于类路径中的文件夹中)

You can run this with:

您可以使用以下命令运行它:

c:\test>java -classpath .\bin foo.Main

Updated source code:

更新的源代码:

package foo;
import java.util.Locale; 
import java.util.ResourceBundle;
import javax.swing.JFrame;

public class Main {

  public static void main(String[] args) {

    Locale currentLocale;
    ResourceBundle messages;

    currentLocale = new Locale("es");

    messages = ResourceBundle.getBundle("resources.Messages", currentLocale);
    System.out.println(messages.getString("Messagesgreetings"));
    System.out.println(messages.getString("Messagesinquiry"));
    System.out.println(messages.getString("Messagesfarewell"));
  }
}

Here as you see, we are loading the properties file with the name "resources.Messages"

如您所见,我们正在加载名为“resources.Messages”的属性文件

Hope this helps.

希望这可以帮助。