找不到资源包异常 [英] Can't find resource bundle exception
问题描述
我想使用名为 strings
的资源包,但在 MainApplication.java
中运行我的 main 方法时出现以下错误:
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) (...)
我的项目结构:
strings.properties
和 strings_de_DE.properties
两个文件与 MainApplication.java
存放在同一个目录下,三个文件都在目录 pi_display/src/logic/
(IntelliJ 将它们放入一个虚拟的Resource Bundle"目录中,以便更清晰地显示项目结构).
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).
主类/方法:
public class MainApplication {
public static void main(String[] args) {
Locale locale = Locale.GERMANY;
ResourceBundle resources = ResourceBundle.getBundle("strings", locale);
// ...
}
我已经尝试将文件移动到其他包并指定包的 baseName
(即 logic.strings
或其他包结构,如果我将它们移动到其他地方).
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).
我不明白为什么在给定 MainApplication
的情况下找不到包,并且包文件在同一目录中.
I don't understand why the bundle can't be found given MainApplication
and the bundle files are in the same directory.
推荐答案
您可能为您的包使用了错误的 baseName
.根据 文档:
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
类加载器"意味着从带有编译类的文件夹加载的资源.在屏幕截图中,资源的路径是 src/logic/strings.properties
,所以它应该位于 classes/logic/
文件夹中,并且作为 baseName 你应该使用logic/strings
(或logic/strings.properties
,我不确定).
"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).
这篇关于找不到资源包异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!