10

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

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Yann Leglise
  • 101
  • 1
  • 1
  • 3
  • Where do you have that file in the JAR? if it stays in a JAR means that you have it in the classpath and you shouldn't have problem to load it normally with ResourceBundle.getBundle – rascio Jun 06 '12 at 09:44
  • I have the same issue, I have a property resource inside a jar file but that jar file is NOT in the programs classpath. I read the javadoc for PropertyResourceBundle and I know I could do that with inputstream but inputstream means IO it will read the file everytime and I lose the default ResourceBundle caching. Is there a way to direct the ResourceBundle class to get the bundle from a resource that is outside of it's classpath ? – MG Developer Apr 10 '15 at 15:53
  • Also the ResourceBundle.Control can be implemented for customization but seems an overkill, better to find ways to put the property files in the classpath. – MG Developer Apr 10 '15 at 16:29

4 Answers4

10

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
cdmckay
  • 31,832
  • 25
  • 83
  • 114
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • The JAR file does not mean it has to be in the classpath. I think the original author is trying to say how to access a resource bundle when outside of the classpath. A way is to use PropertyResourceBundle with InputStream but then you lose the caching and anytime you do bundle you have to do a new Bundle. I have the same issue, I have a resource that is not in the classpath. – MG Developer Apr 10 '15 at 15:56
1

Have you tried this?

ResourceBundle.getBundle("conf/messages", java.util.Locale.getDefault());
adarshr
  • 61,315
  • 23
  • 138
  • 167
0

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

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

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

0

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:

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

To read this file:

import java.util.ResourceBundle;

....

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

Thanks

Arun
  • 2,360
  • 2
  • 17
  • 23