0

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

4

1 回答 1

2

我通过使用类加载器来实现这一点。来源是

 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);
于 2012-08-24T09:30:31.880 回答