Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading with ResourceBundle from inside a 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.

Here is the configuration:

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());

If the files were on disk, in a directory referenced by the program classpath, this works fine. But I don't know how I can manage combining use of ResourceBundle.getBundle and use of resources from within 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, which is not very smart).

like image 359
Yann Leglise Avatar asked Jun 06 '12 09:06

Yann Leglise


1 Answers

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());

The javadoc says:

baseName - the base name of the resource bundle, a fully qualified class name
like image 195
JB Nizet Avatar answered Oct 01 '22 17:10

JB Nizet