简体   繁体   English

Java ResourceBundle.getBundle如何加载属性文件?

[英]How does Java ResourceBundle.getBundle load properties file?

I spent some time trying to load the properties file and couldn't find any solution that worked. 我花了一些时间尝试加载属性文件,但找不到任何有效的解决方案。 Here is a picture of my setup. 这是我的设置的图片。

Properties file location: /sandbox/resources/properties/MessageBundle_en_US.properties 属性文件位置:/sandbox/resources/properties/MessageBundle_zh_CN.properties

App location: /sandbox/src/main/java/com/app/sandbox/App.java 应用程序位置:/sandbox/src/main/java/com/app/sandbox/App.java

在此处输入图片说明

Not sure what is going on. 不知道发生了什么。 I tried several combinations, but it just can't get past the exception. 我尝试了几种组合,但无法克服异常。 I even tried entire path of the folder. 我什至尝试了文件夹的整个路径。 Can anyone tell me how ResourceBundle.getBundle() method looks for properties files? 谁能告诉我ResourceBundle.getBundle()方法如何查找属性文件?

UPDATE 1: This solution stopped working a few minutes later: 更新1:此解决方案在几分钟后停止工作:

ResourceBundle mybundle = ResourceBundle.getBundle("MessageBundle");

UPDATE 2: The solution above no longer worked during my last run. 更新2:以上解决方案在我上次运行时不再起作用。 So I changed it to: 所以我将其更改为:

ResourceBundle mybundle = ResourceBundle.getBundle("properties/MessageBundle");

And it worked. 而且有效。 It seems there might be a caching issue with how ResourceBundle looks for the properties file. 似乎ResourceBundle如何查找属性文件可能存在缓存问题。

I am still not sure how ResourceBundle class looks for the properties file. 我仍然不确定ResourceBundle类如何查找属性文件。 It says to pass a "baseName" parameter: 它说要传递“ baseName”参数:

baseName - the base name of the resource bundle, a fully qualified class name

https://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html#getBundle(java.lang.String) https://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html#getBundle(java.lang.String)

But it's not clear how it starts looking for the files. 但目前尚不清楚它如何开始查找文件。 Does it start looking from the root of the project directory? 它是否从项目目录的根目录开始查找? If someone can answer this, it would be good. 如果有人可以回答这个问题,那就太好了。 What does it mean by a fully qualified class name . 完全限定的类名是什么意思。 I wouldn't think "properties.MessageBundle" is fully qualified. 我不认为“ properties.MessageBundle”是完全合格的。

尝试如下使用;

ResourceBundle bundle = ResourceBundle.getBundle("properties/MessageBundle");

Yes, this should be the way: 是的,这应该是这样的:

R`esourceBundle mybundle = ResourceBundle.getBundle("MessageBundle");`

The reason that it can find it in the properties package without specifying the whole path, is because Java knows that the bundles by default should be in that package, and it doesn't need the other parts. 它无需指定整个路径就可以在属性包中找到它的原因是因为Java知道默认情况下捆绑包应该在该包中,并且不需要其他部分。 So, if you use the whole path like: "/sandbox/resources/properties/MessageBundle" , Java will interpret it like: 因此,如果您使用整个路径,例如: "/sandbox/resources/properties/MessageBundle" ,则Java会将其解释为:

"/sandbox/resources/properties/MessageBundle/sandbox/resources/properties/MessageBundle"

Which is clearly not correct. 这显然是不正确的。 So, if you need to create more packages inside the properties package, just go from there and not from the root of your project. 因此,如果您需要在properties包中创建更多包,只需从那里而不是从项目的根目录开始。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java ResourceBundle.getBundle()不明确 - java ResourceBundle.getBundle() undeterministic 如何忽略ResourceBundle.getBundle中的defaultLocale? - How to ignore defaultLocale in ResourceBundle.getBundle? Java ResourceBundle.getBundle缺少资源异常 - Java ResourceBundle.getBundle missing resource exception ResourceBundle.getBundle和Websphere - ResourceBundle.getBundle and Websphere 为什么ResourceBundle.getBundle(String str)无法从Websphere中的默认属性文件中找到值 - Why ResourceBundle.getBundle(String str) cannot find values from default properties file in Websphere ResourceBundle.getBundle(“ResourceFile”,新的Locale(“us”,“US”))在哪里查找文件? - Where does ResourceBundle.getBundle(“ResourceFile”, new Locale(“us”, “US”)) look for the file? 无法使用 ResourceBundle.getBundle() 加载 ResourceBundle 找不到基本名称的包,区域设置 en_US - Unable to load ResourceBundle with ResourceBundle.getBundle() Can't find bundle for base name , locale en_US Java 1.5,Java EE 5,WAS 6.1:使用ResourceBundle.getBundle(…)加载资源束的异常 - Java 1.5, Java EE 5, WAS 6.1: Exception loading a resource bundle with ResourceBundle.getBundle(…) 使用类而不是属性文件或 ListResourceBundles 时 ResourceBundle.getBundle() 的强制语言 - Mandatory language for ResourceBundle.getBundle() when using classes instead of properties files or ListResourceBundles getbundle(basename)ResourceBundle java - getbundle(basename) ResourceBundle java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM