Search code examples
javaresourcebundle

java.util.MissingResourceException in Resource bundle


Hello All, I am creating a ResourceBundle to load the property files. My file structure looks like

| ---Main

   |
    ----ResourceBundleLoad.java

| --Resource

   |
    --- resourcebundle.properties

In normal when i put main class and the property file in same package means it retrieves all the property file values. If i separate both files means it's not working. It throws java.util.MissingResourceException exception.

My code is

 private static final String BUNDLE_NAME = "ExternalizedLogMessages";
 private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);

Please suggest me how to solve this problem


Solution

  • I achieve this by using the class loader. the source is

     private static URLClassLoader resourceLoader= null;
    
    /**
     * Initialize class loader.
     */
    static{
        ClassLoader currentThreadClassLoader
         = Thread.currentThread().getContextClassLoader();
    
        //assuming that current path is the project directory
        try {
            resourceLoader
             = new URLClassLoader(new URL[]{new File(".").toURI().toURL()},
                                  currentThreadClassLoader);
        } catch (MalformedURLException e) {
            logger.error(e);
        }   
    }
    
    /**
     * Properties bundle name.
     */
    private static final String BUNDLE_NAME = "resource.ExternalizedLogMessages"; //$NON-NLS-1$
    
    /**
     * Resource bundle object.
     */
    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
            .getBundle(BUNDLE_NAME,Locale.US,resourceLoader);