java 从 jar 中加载 ResourceBundle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10911708/
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-10-31 03:03:59  来源:igfitidea点击:

Loading with ResourceBundle from inside a jar

javajarresourcebundle

提问by Yann Leglise

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.

我已经看到了一些关于如何通过getResourceAsStream在 jar 中加载特定文件的答案,我可以管理它。但是,我面临着一些非常具体的问题,我在论坛上找不到有关此问题的答案。

Here is the configuration:

这是配置:

I have a jar file having a confdirectory that contains 2 properties files messages_en_US.propertiesand messages_fr_FR.properties. The classical way to load such resources is to use

我有一个 jar 文件,其中包含 2 个属性文件messages_en_US.propertiesmessages_fr_FR.propertiesconf目录。加载此类资源的经典方法是使用

ResourceBundle.getBundle("messages", java.util.Locale.getDefault());

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.getBundleand 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).

如果文件在磁盘上,在程序类路径引用的目录中,这可以正常工作。但我不知道如何管理ResourceBundle.getBundle 的组合使用和 jar中资源的使用。事实上,因为我无法通过getResourceAsStream看到任何桥梁(或者这意味着我自己管理语言环境以指定整个资源文件名,这不是很聪明)。

Can anyone help ?

任何人都可以帮忙吗?

Thanks.

谢谢。

回答by JB Nizet

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

如果它位于 jar 内的 conf 目录中,那么您尝试加载的包的包是conf,您应该使用

ResourceBundle.getBundle("conf.messages", java.util.Locale.getDefault());

The javadocsays:

javadoc说:

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

回答by adarshr

Have you tried this?

你试过这个吗?

ResourceBundle.getBundle("conf/messages", java.util.Locale.getDefault());

回答by adarshr

The ResourceBundle always search for a properties file, so, if you run :

ResourceBundle 始终搜索属性文件,因此,如果您运行:

ResourceBundle.getBundle("messages", java.util.Locale.getDefault());

The ResourceBundle will search for the "messages.properties" in your classpath.

ResourceBundle 将在您的类路径中搜索“messages.properties”。

回答by Arun

Assuming file messages.properties is part of some xyz.jar which is already on classpath of the project where you want to use this. Packaging of this file in jar is as below:

假设文件messages.properties 是一些xyz.jar 的一部分,它已经在你想要使用它的项目的类路径上。这个文件在jar中的打包如下:

src
   |main
       |resources
            |com
                |xyz
                   ....|message.properties 

To read this file:

要读取此文件:

import java.util.ResourceBundle;

....

....

ResourceBundle.getBundle("com.xyz.resources.messages", Locale.getDefault());

Thanks

谢谢