从jar内部加载ResourceBundle [英] Loading with ResourceBundle from inside a jar
问题描述
我已经看到了一些关于如何通过 getResourceAsStream 在jar中加载特定文件的答案,我可以管理它。
但是我面对的是一些非常具体的内容,我在论坛上找不到相关答案。
I have seen some answers concerning how to load a particular file within a jar through getResourceAsStream, and I can manage this. However I am facing something really specific an I could not find an answer about this on the forum.
这是配置:
我有一个包含 conf 目录的jar文件,其中包含2个属性文件 messages_en_US.properties 和 messages_fr_FR.properties 。加载此类资源的经典方法是使用
I have a jar file having a conf directory that contains 2 properties files messages_en_US.properties and messages_fr_FR.properties. The classical way to load such resources is to use
ResourceBundle.getBundle("messages", java.util.Locale.getDefault());
如果文件在磁盘上,则在programm类路径引用的目录中,这样可以正常工作。但我不知道如何管理 ResourceBundle.getBundle 的使用以及从jar中使用资源。
确实,因为我看不到通过 getResourceAsStream 的任何桥接(或者这意味着我自己管理语言环境以指定整个资源文件名,这不是很聪明)。
If the files were on disk, in a directory referenced by the programm classpath, this works fine. But I don't know how I can manage combining use of ResourceBundle.getBundle and use of resources from witin a jar. Indeed, as I cannot see any bridge through getResourceAsStream (or this would imply managing locale by myself to specify the entire resource file name, wich is not very smart).
有人可以帮忙吗?
谢谢。
推荐答案
如果它位于jar内的conf目录中,那么你想要加载的bundle的包是 conf
,你应该使用
If it's in a conf directory inside the jar, then the package of the bundle you're trying to load is conf
, and you should use
ResourceBundle.getBundle("conf.messages", java.util.Locale.getDefault());
javadoc 说:
baseName - the base name of the resource bundle, a fully qualified class name
这篇关于从jar内部加载ResourceBundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!