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, the default locale, and the caller's class loader.

Syntax

public static final ResourceBundle getBundle(String baseName)

Parameters

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

Return Value

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

Exception

  • Throws NullPointerException, if baseName 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 a Java program.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a resource bundle in default locale
    ResourceBundle bundle = ResourceBundle.getBundle("Greetings");

    //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