Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does ResourceBundle.getBundle("ResourceFile", new Locale("us", "US")) look for the file?

I'm running Eclipse and trying to create a simple test program trying our ResourceBundle with a couple of different files. The file is properly named as ResourceFile_us_US.properties. But I'm getting an exception on the getBundle() call because it apparently can't find the file. Where should it be located so it can be found?

like image 545
Craig A Avatar asked Feb 26 '10 20:02

Craig A


People also ask

How does ResourceBundle getBundle work?

getBundle. Gets a resource bundle using the specified base name, locale, and class loader. This method behaves the same as calling getBundle(String, Locale, ClassLoader, Control) passing a default instance of ResourceBundle.

How do you create a resource bundle in Java?

To create the ResourceBundle , invoke the getBundle method, specifying the base name and Locale : ResourceBundle labels = ResourceBundle. getBundle("LabelsBundle", currentLocale); The getBundle method first looks for a class file that matches the base name and the Locale .


4 Answers

You know java is looking for a properties file in a specific locale. You may be baffled why java keeps complaining it can't find a properties file that is right there. A few things to keep in mind when debugging this type of errors:

  1. These resource properties files are loaded by classloader, similar to java classes. So you need to include them in your runtime classpath.

  2. These resources have fully-qualified-resource-name, similar to a fully-qualified-class-name, excerpt you can't import a resource into your java source file. Why? because its name takes the form of a string.

  3. ResourceBundle.getBundle("config") tells the classloader to load a resource named "config" with default package (that is, no package). It does NOT mean a resource in the current package that has the referencing class.

  4. ResourceBundle.getBundle("com.cheng.scrap.config") tells the classloader to load a resource named "config" with package "com.cheng.scrap." Its fully-qualified-resource-name is "com.cheng.scrap.config"

More : Can't find bundle for base name com...config, locale zh_CN

Cheers.

like image 126
tmwanik Avatar answered Oct 08 '22 14:10

tmwanik


One of two:

  1. /src/resources Make sure you include locale in name of resource bundle file. e.g for Zimbabwe, it will be ResourceBundle_en_ZW.properties and you would load it as ResourceBundle resourceBundle = ResourceBundle.getBunndle("ResourceBundle", Locale.getDefault());

  2. In your classpath. Make sure the calsspath environment variable is set

like image 45
Shinx Avatar answered Oct 08 '22 12:10

Shinx


If you create a package resources and put the file hello_en_US.properties inside it, which has the content:

hello = Hello World!!

you can print the content of hello using the following code:

package localization;

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleDemo {

public static void main(String[] args) {

    Locale en_US = new Locale("en", "US");
    ResourceBundle bundle = ResourceBundle.getBundle("resources.hello", en_US);

    // print the value of the key "hello"
    System.out.println("" + bundle.getString("hello"));

  }
}
like image 26
Adil Avatar answered Oct 08 '22 14:10

Adil


I believe it just looks in the classpath.

like image 25
Alex Beardsley Avatar answered Oct 08 '22 12:10

Alex Beardsley