Java Utility Library

Java ResourceBundle - getBundle() Method



The java.util.ResourceBundle.getBundle() method is used to get a resource bundle using the specified base name, locale, and class loader.

Syntax

public static ResourceBundle getBundle(String baseName,
                                       Locale locale,
                                       ClassLoader loader)

Parameters

baseName Specify the base name of the resource bundle, a fully qualified class name.
locale Specify the locale for which a resource bundle is desired.
loader Specify the class loader from which to load the resource bundle.

Return Value

Returns a resource bundle for the given base name and locale.

Exception

  • Throws NullPointerException, if baseName, locale, or loader is null.
  • Throws MissingResourceException, if no resource bundle for the specified base name can be found.

Example:

Lets assume that we have resource file called Greetings_en_US.properties in the CLASSPATH with the following content. This file contains local message for US country.

greet = Hello World!

The example below shows how to use java.util.ResourceBundle.getBundle() method to get the resource bundle Greetings_en_US.properties in specified locale and class loader in a Java program.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    ClassLoader cl = ClassLoader.getSystemClassLoader();

    //creating a resource bundle in specified locale and class loader
    ResourceBundle bundle = ResourceBundle.getBundle("Greetings", Locale.US, cl);

    //printing the text assigned to "greet" 
    //key in the bundle
    System.out.println(bundle.getBundle("greet"));   
  }
}

The output of the above code will be:

Hello World!

❮ Java.util - ResourceBundle